views:

160

answers:

4

First, let me specify I am refering to the List<T> method and not the C# keyword. What would be Microsoft's reasoning for having the Foreach method on the List<T> collection but no other collection/enumerable type, specifically IEnumerable<T>?

I just discovered this method the other day, and found it to be very nice syntax for replacing traditional foreach loops that only perform one or 2 lines of methods on each object.

It seems like it would be fairly trivial to create an extension method that ,performs this same function. I guess I'm looking at why MS made this decision and based on that if I should just make an extension method.

+10  A: 

Eric Lippert has responded to this question numerous times, including on his blog.

His response has been that ForEach() is something you use explicitly for its side effects - whereas LINQ is largely intended to be a side-effect free API to define projections and manipulations on sequences.

Outside of uses like parallel loops, the functional ForEach syntax doesn't add much in the way of expressive power, and it is slightly less efficient than a regular foreach loop due to the delegate invocations.

If you really, really want a ForEach for IEnumerable<T>, it's not hard to write one:

public static class ForEachExtension
{
    public static void ForEach<T>( this IEnumerable<T> seq, Action<T> action )
    {
        foreach( var item in seq ) 
            action( item );
    }

    // Here's a lazy, streaming version:
    public static IEnumerable<T> ForEachLazy<T>( this IEnumerable<T> seq, Action<T> action )
    {
        foreach( var item in seq )
        {
            action( item );
            yield return item;
        }
    }
}
LBushkin
+1 - ForEach() on an IEnumerable has more cons than pros
Rex M
Also, use Select. It accomplishes nearly the same thing in a LINQ-ish way :)
Rex M
+2  A: 

The wrote this prior to extension methods - it was added in .NET 2.0. At the time, there was no way to add this as an extension to any enumerable - it would have to be part of the interface (which I agree doesn't necessarily fit, since enumerables can be infinitely long, and this would be bad).

I believe that, when C# 3 was released, they probably felt that LINQ, via Select, privided an easy alternative, while being more clear about side-effects. Eric Lippert's blog post on the subject describes the side-effects, and rationale behind why you'd use ForEach vs. foreach in more detail, as well.

Reed Copsey
+3  A: 

Read the answer from the man who helped design the compiler .. http://blogs.msdn.com/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx

flesh
+1  A: 

To crib an answer from Raymond Chen (who was cribbed from Eric Gunnerson):

Every feature starts off at -100 points.

Basically, somebody would have to write it and by so doing take time away from some other feature. Since you can create ForEach() yourself trivially it would never take precedence over other, less trivial features.

Plus ForEach() predates extension methods.

Kevin Montrose