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?
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.