LINQ in Action - LINQ Book & News

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)
    action(item);
}

Published Wednesday, February 25, 2009 7:19 PM by Fabrice Marguerie

Comments

 

LINQ in Action - LINQ Book & News said:

Eric Lippert , whose blog you shouldn't miss, adds his own arguments to the debate about whether using

May 21, 2009 3:15 AM
 

Fabrice's weblog said:

Eric Lippert , whose blog you shouldn't miss, adds his own arguments to the debate about whether using

May 21, 2009 3:16 AM
New Comments to this post are disabled