C#的foreach VS每个功能 [英] C# foreach vs functional each

查看:112
本文介绍了C#的foreach VS每个功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其中的一个,你更喜欢哪个?



 的foreach(僵尸变种僵尸)
{
zombie.ShuffleTowardsSurvivors();
zombie.EatNearbyBrains();
}

  zombies.Each(僵尸= GT; {
zombie.ShuffleTowardsSurvivors();
zombie.EatNearbyBrains();
});


解决方案

第二种形式。



在我看来,少的语言结构和关键字,你必须使用,效果更佳。 C#中有足够的多余的污物,因为它是。



一般情况下,少你必须输入,就更好了。说真的,怎么可能不希望在这样的情况下使用无功?当然,如果是明确的是你唯一的目标,你仍然可以使用匈牙利命名法...你有一个IDE,让如果你使用ReSharper的,只要你将鼠标悬停在您输入信息......或者,当然按Ctrl + Q ..



@TED委托调用的性能影响是次要问题。如果你这样做了一千方面肯定,运行点跟踪,看看它是否是不能接受的。



@Reed科普塞:重新非标准,如果开发人员可以'科技工作出了什么。每个做那么你有更多的问题,嘿。黑客攻击的语言,使之更好的是编程的伟大的乐趣之一。


Which one of these do you prefer?

foreach(var zombie in zombies)
{
    zombie.ShuffleTowardsSurvivors();
    zombie.EatNearbyBrains();
}

or

zombies.Each(zombie => {
    zombie.ShuffleTowardsSurvivors();
    zombie.EatNearbyBrains();
});

解决方案

Second form.

In my opinion, the less language constructs and keywords you have to use, the better. C# has enough extraneous crud in it as it is.

Generally the less you have to type, the better. Seriously, how could you not want to use "var" in situations like this? Surely if being explicit was your only goal, you'd still be using hungarian notation... you have an IDE that gives you type information whenever you hover over... or of course Ctrl+Q if you're using Resharper...

@T.E.D. The performance implications of a delegate invocation are a secondary concern. If you're doing this a thousand terms sure, run dot trace and see if it's not acceptable.

@Reed Copsey: re non-standard, if a developer can't work out what ".Each" is doing then you've got more problems, heh. Hacking the language to make it nicer is one of the great joys of programming.

这篇关于C#的foreach VS每个功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆