A: 

No, there is no such extension method. List<T> exposes a real method called ForEach (which has been there since .NET 2.0).

Andrew Hare
+3  A: 

Nope, there isn't a built-in ForEach extension method. It's easy enough to roll your own though:

public static class EnumerableExtensions
{
    public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
    {
        if (source == null) throw new ArgumentNullException("source");
        if (action == null) throw new ArgumentNullException("action");

        foreach (T item in source)
        {
            action(item);
        }
    }
}

But why bother? As Eric Lippert explains in this blog post, the standard foreach statement is more readable -- and philosophically more appropriate -- than a side-effecting ForEach method:

myEnumerable.ForEach(x => Console.WriteLine(x));
// vs
foreach (var x in myEnumerable) Console.WriteLine(x);
LukeH
But it's really stupid if I want to do this: var results = myenumerable.Where(...).Select(...)foreach(var result in results){ .... }I would rather do somethign like this instead: myenumerable.Where(...).Select(...).ForEach(...)
Micah
@Micah: Well, if you want to do that then just go ahead and use the extension method in my answer.
LukeH
@Micah: If you would rather do that, then do that. It's a *single line of code in an extension method*. What's stopping you?
Eric Lippert
@Eric: I've already done it, I just still don't get the rationale behind why not to include it as part of the framework. Especially considering the guys who wrote List<T> and .AsParallel() thought it was worth including.
Micah
@Micah: List<T>.ForEach adds value; because it can use internal implementation details of List<T> it can optimize its implementation. (Whether it actually does so or not I do not know.) The parallel ForEach adds value; it can execute a sequence of actions in parallel. Neither of these value-adds are available for an extension method on IEnumerable<T>, which cannot see internal implementation details and does not execute in parallel. It adds neither value nor representational power; it's just a confusing and possibly slower way to rewrite perfectly sensible looping code.
Eric Lippert
A: 

There's some talk about it here. It's not build in to the frame work, but you can roll your own extension method and use it that way. This is probably your best bet from what I can tell.

I've been in the same situation.

public static class Extensions {
  public static void ForEach<T>(this IEnumerable<T> source, Action<T> action) {
    foreach (var item in source) {
      action(item);
    }
  }
}
Robert Greiner
+2  A: 

No there isn't. Eric Lippert talks about why this feature was omitted in his blog:

A number of people have asked me why there is no Microsoft-provided “ForEach” sequence operator extension method.

As a summary the two main reasons are:

  • Using ForEach violates the functional programming principles that all the other sequence operators are based upon - no side-effects.
  • A ForEach method would add no new representational power to the language. You can easily achieve the same effect more clearly using a foreach statement.
Mark Byers
That's a stupid reason. You can achieve all the functionality of the linq extensions using a for each loop.
Micah
@Micah: Sure and you can also achieve the same effect as any LINQ extension method by writing it directly in CIL, but I don't think it's **more clear**.
Mark Byers
What's so unclear about myEnumerable.ForEach(...)?
Micah
@Micah: It doesn't fit the pattern of the rest of LINQ. What's exactly don't you like about the foreach statement? It says exactly what you want to do in a clear way, no need for lambda expressions. It's easy to nest them. But if you feel that `ForEach` is more readable then don't let me try to stop you.
Mark Byers
It seems stupid when you chain a bunch of calls together like myEnumerable.Where(...).Select(...) to not just add a .ForEach(...) to the very end of it instead of having to write a seperate "foreach" code block.
Micah
Also, if it's so bad to do it, why did the PLinq team implement it? .AsParallel().ForAll(...)
Micah
@Micah: `ForAll` is not the same as what you are asking for - it doesn't work on any IEnumerable - it only works on parallel queries. And there is no `forall` keyword that does the same as it. *Although ForAll may have philosophical issues, there is a pragmatic reason to include this method. Without ForAll, we would take a fairly serious performance hit in many situations.* See: http://reedcopsey.com/2010/02/03/parallelism-in-net-part-8-plinqs-forall-method/
Mark Byers
@Micah: That article actually mentions that they have a ForEach too: http://msdn.microsoft.com/en-us/library/dd992001.aspx
Mark Byers
I wish foreach of you to take it easy and forall of you to stop flagging each other's comments.
Will
A: 

IEnumerable do not have this extension method.

Read Eric Lippert's blog here for the reasoning behind it.

So if you need it, you will hav eto write it yourself :)

Øyvind Bråthen