LINQ's ForEach doesn't work on IEnumerable<T>

29 Mar 2009 09:39

For some reason, LINQ’s ForEach extension method doesn’t work on IEnumerable<T>; it only works on IList<T>. Easy fix:

public static class EnumerableExtensions
{
    public static void ForEach<T>(this IEnumerable<T> values, Action<T> action)
    {
        foreach (var value in values)
        {
            action(value);
        }
    }
}