why ForEach Linq Extension on List rather than on IEnumerable

Muhammad Adeel Zahid picture Muhammad Adeel Zahid · Apr 18, 2011 · Viewed 11.7k times · Source

Possible Duplicate:
Why is there not a ForEach extension method on the IEnumerable interface?

Hello,

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?

Answer

Mark Cidade picture Mark Cidade · Apr 18, 2011

List<T>.ForEach() isn't an extension method. It's just a method on List<T>.

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.