Parallel IEnumerable foreach

Here is a quick way (using an Extension method) to to use Parallel programming with any object implements IEnumerable<T>in C#

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

Code usage example:

var names = new List<string> { "Sammy", "Sean", "Josh", "James", "Mike" };
names.ParallelForEach(Console.WriteLine);
Console.Read();

Add comment