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"));
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted @ Thursday, December 04, 2008 7:13 PM
Print
«February»
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910