为什么没有对的IEnumerable没有的ForEach扩展方法? [英] Why there is no ForEach extension method on IEnumerable?

查看:191
本文介绍了为什么没有对的IEnumerable没有的ForEach扩展方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由另外一个问题问的是缺少邮编启发功能:

Inspired by another question asking about the missing Zip function:

为什么会出现在可枚举类没有的ForEach 扩展方法?或任何?这得到了的ForEach 方法的唯一类是名单,其中;> 。还有一个原因,为什么它的缺失(性能)?

Why is there no ForEach extension method in the Enumerable class? Or anywhere? The only class that gets a ForEach method is List<>. Is there a reason why it's missing (performance)?

推荐答案

目前已包括在完成这一工作语言中的foreach语句的大部分时间。

There is already a foreach statement included in the language that does the job most of the time.

我讨厌看到以下内容:

list.ForEach( item =>
{
    item.DoSomething();
} );

相反的:

foreach(Item item in list)
{
     item.DoSomething();
}

后者是更清晰,更易于阅读的在大多数情况下,虽然也许有点更长的时间来输入。

The latter is clearer and easier to read in most situation, although maybe a bit longer to type.

不过,我必须承认,我改变了我的立场,在这个问题上;一个foreach()扩展方法确实是在某些情况下非常有用。

However, I must admit I changed my stance on that issue; a ForEach() extension method would indeed be useful in some situations.

下面是语句和方法之间的主要区别:

Here are the major differences between the statement and the method:

  • 类型检查:的foreach是在运行,的ForEach()是在编译时(!大加)
  • 要调用一个委托的语法确实要简单得多:objects.ForEach(DoSomething的);
  • 的ForEach()可以被链接:。虽然这样的功能邪恶/实用性有待商榷

这些是很多人在这里所做的所有伟大的观点,我能看到为什么人们缺少的功能。我不会介意微软加入了标准的ForEach方法,在未来框架的迭代。

Those are all great points made by many people here and I can see why people are missing the function. I wouldn't mind Microsoft adding a standard ForEach method in the next framework iteration.

这篇关于为什么没有对的IEnumerable没有的ForEach扩展方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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