0

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?

Community
  • 1
  • 1
GaryX
  • 737
  • 1
  • 5
  • 20

3 Answers3

2

Frequently asked, definitively answered:

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

Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
1

List.ForEach

Flagbug
  • 2,093
  • 3
  • 24
  • 47
0

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.

Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151