可能重复: Why是不是有IEnumerable接口上一个foreach扩展方法?
您好,
我的问题是,为什么的Foreach扩展方法是对列表,而不是IEnumreable定义。我已阅读埃里克利珀的文章但问题是,如果是如此糟糕,有这样的方法比为什么会在那里列表?
My question is why Foreach extension method is defined on List rather than IEnumreable. i have read Eric Lippert's article but the point is, if it is so bad to have such method than why is it there for List?
名单,其中,T> .ForEach()
不是一个扩展方法。这只是对列表℃的方法; T>
List<T>.ForEach()
isn't an extension method. It's just a method on List<T>
.
其中一个主要的原因,这是不具备的LINQ(即可枚举
)是LINQ查询应该是免费的副作用(所以你可以,例如,执行一个查询多次,得到相同的结果不会更改),这让他们很可组合。所有的LINQ操作接受委托对 Func键
;他们没有接受动作
委托。
One of the main reasons that it isn't available in LINQ (i.e., Enumerable
) is that LINQ queries are supposed to be free of side-effects (so you can, e.g., execute a query multiple times and get the same results without changing them), which makes them very composable. All the LINQ operations that accept delegates are for Func
; none of them accept an Action
delegate.