I did a quick experiment to see how easy it would be to write a .Each() method which I could use to operate on an IEnumerable. It turned out to be quite simple.


using System;
using System.Collections.Generic;

public static class IEnumerableExtensions
{
    public static void Each<T>(this IEnumerable<T> collection, Action<T> action)
    {
        foreach (var item in collection)
        {
            action(item);
        }
    }

    public static void EachWithIndex<T>(this IEnumerable<T> collection, Action<T, int> action)
    {
        int i = 0;

        foreach (var item in collection)
        {
            action(item, i++);
        }
    }
}

public class Test
{
    public static void Main()
    {
        var items = new[] {'a', 'b', 'c'};

        items.Each(Console.WriteLine);
        items.EachWithIndex((item, index) => Console.WriteLine("{0}: {1}", index, item));

        Console.ReadLine();
    }
}