开发者

why .net c# lack of "EACH" [duplicate]

This question already has answers 开发者_运维技巧here: Closed 11 years ago.

Possible Duplicate:

is there a .Each() (or .ForEach() ) iterator in the .Net standard library?

Maybe I am wrong, but I don't see the "EACH" function in .net (3.0)?

We can

IEnumerable<T>.Select().Where().First().Any()

Why don't we have

IEnumberable<T>.Each(Action<T> action)?

So instead of 2 lines: Foreach(T t in list) action(t);

we can just call list.Each((t)=>{blah;blah;})?

Is there a concern for performance?


Frequently asked, definitively answered:

http://blogs.msdn.com/b/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx


List.ForEach


It was made this way, since none of the extention methods on IEnumerable was supposed to have any side effects, and a ForEach would have side effects as it's primary objective.

It was deemed that a foreach loop is not much more verbose to write out.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜