Ruby-like each statement in C#

Funny this isn't in the provided Enumerable extension methods.

In any case, here it is:

        internal static void Each<T>(this IEnumerable<T> @this, Action<T> action)
        {
            foreach(T t in @this)
                action(t);
        }

        internal static void Each<T>(this IEnumerable @this, Action<T> action)
        {
            foreach (object t in @this)
                if (t is T)
                {
                    action((T)t);
                }
        }

An example:

   objects.Each(x => DoSomethingFunky(x, "some parameter"));

posted @ Thursday, December 04, 2008 7:13 PM

Print
«November»
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345