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.
精彩评论