为什么的ForEach LINQ的扩展名列表上,而不是IEnumerable的 [英] why ForEach Linq Extension on List rather than on IEnumerable

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

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/101265/why-is-there-not-a-foreach-extension-method-on-the-ienumerable-interface">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&GT; .ForEach()不是一个扩展方法。这只是对列表℃的方法; T&GT;

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.

这篇关于为什么的ForEach LINQ的扩展名列表上,而不是IEnumerable的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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