Fabrice's weblog

Tools and Source


My .NET Toolbox
An error occured. See the script errors signaled by your web browser.
No tools selected yet
.NET tools by SharpToolbox.com

Read sample chapters or buy LINQ in Action now!
Our LINQ book is also available on AMAZON

.NET jobs

Emplois .NET


ASP.NET Hosting transatlantys




Selected content


ForEach or not ForEach, that is the question

In LINQ in Action, we discuss about the missing ForEach query operator. This is in Chapter 5 "Beyond basic in-memory queries", more precisely in section 5.2.2. There, we indicate that Eric White suggested this operator in his functional programming tutorial, although I'm not able to find the exact reference at the moment in this tutorial.

Since then, a lot of people have been asking for ForEach. This can be seen on Kirill Osenkov's blog, where you'll find links to discussions about whether ForEach is good or bad.
I don't know if we're going to see ForEach appear in .NET. Anyway, it's not very difficult to write your own:

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 (var item in source)

Cross-posted from http://LinqInAction.net


Joe Chung said:

Maybe the LINQ team was "deferring its execution" of the ForEach implementation until PLINQ and Parallel.ForEach in .NET 4.0?

# February 25, 2009 3:23 PM

Fabrice Marguerie said:

Could be :-)

It doesn't have to be parallel though.

# February 25, 2009 3:51 PM