Tigraine
Daniel Hoelbling talks about .NET

IEnumerable<T>.Each as C# Extension Method

February 11th, 2009 . by Daniel Hölbling

A very long time ago I went through the Ruby in 20 minutes tutorial when I saw this:

@names.each do |name|
  puts "Hallo, #{name}!"
end

When C# came out later I always wondered why there is no functional equivalent on the IEnumerable<T> interface, since it would be a perfect place for quick inline method calls without having to write a full foreach statement.

At that time my knowledge of extension methods and delegates was too limited to do this myself, but that doesn’t mean it has to stay that way.

I finally remembered that I never got to it last time and implemented it today.

Oh and it’s so damn easy too:

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

To use this .Each method now you simply need to be using the Namespace where the EachExtension is in and you can write code like this:

IEnumerable<string> x = new[] {"hello", "world", "how", "are", "you"};
x.Each(Console.WriteLine);

Or with multiple parameters:

IEnumerable<string> x = new[] {"hello", "world", "how", "are", "you"};
x.Each(p => Console.WriteLine("{0}", p));

Or, even whole inline method bodies:

IEnumerable<string> x = new[] {"hello", "world", "how", "are", "you"};
x.Each(p =>
           {
               Console.Write("Word:");
               Console.WriteLine(p);
           });

So, Lambdas are fun after all :)


View Comments to “IEnumerable<T>.Each as C# Extension Method”


  1. [...] complained before that there are operators like the IEnumerable<T>.Each() are somewhat missing from the default implementation of Linq, but I never really gave it too much [...]

  2. comment number 2 by: Matt Sherman

    This is great…I was just going to send a note to the C# guys to suggest this. Each() is a fundamental expression in Ruby and Javascript, it should be part of C# if only to keep up with the trends. :)

  3. comment number 3 by: Matt Sherman

    Here's a tweak which returns the enumerable, which would allow chaining:

    public static IEnumerable Each<T>(this IEnumerable<T> enumberable, Action<T> action)
    {
    foreach (var item in enumberable) {
    action(item);
    }
    return enumberable;
    }

  4. comment number 4 by: Daniel Hu00f6lbling

    You could even extend this to allow for transformations like this:nnIEnumerable x = new[] { “hello”, “world”, “how”, “are”, “you” };nIEnumerable enumerable = x.Each(p => p.Length);nnCode:nnpublic static IEnumerable Each(this IEnumerable enumberable, Func action)n{n foreach (var item in enumberable)n {n yield return action(item);n }n}

  5. comment number 5 by: Daniel Hu00f6lbling

    You could even extend this to allow for transformations like this:nnIEnumerable x = new[] { “hello”, “world”, “how”, “are”, “you” };nIEnumerable enumerable = x.Each(p => p.Length);nnCode:nnpublic static IEnumerable Each(this IEnumerable enumberable, Func action)n{n foreach (var item in enumberable)n {n yield return action(item);n }n}

  6. comment number 6 by: Daniel Hu00f6lbling

    You could even extend this to allow for transformations like this:nnIEnumerable x = new[] { “hello”, “world”, “how”, “are”, “you” };nIEnumerable enumerable = x.Each(p => p.Length);nnCode:nnpublic static IEnumerable Each(this IEnumerable enumberable, Func action)n{n foreach (var item in enumberable)n {n yield return action(item);n }n}

  7. comment number 7 by: Daniel Hu00f6lbling

    You could even extend this to allow for transformations like this:nnIEnumerable x = new[] { “hello”, “world”, “how”, “are”, “you” };nIEnumerable enumerable = x.Each(p => p.Length);nnCode:nnpublic static IEnumerable Each(this IEnumerable enumberable, Func action)n{n foreach (var item in enumberable)n {n yield return action(item);n }n}

  8. comment number 8 by: Daniel Hu00f6lbling

    You could even extend this to allow for transformations like this:nnIEnumerable x = new[] { “hello”, “world”, “how”, “are”, “you” };nIEnumerable enumerable = x.Each(p => p.Length);nnCode:nnpublic static IEnumerable Each(this IEnumerable enumberable, Func action)n{n foreach (var item in enumberable)n {n yield return action(item);n }n}

  9. comment number 9 by: Daniel Hu00f6lbling

    You could even extend this to allow for transformations like this:nnIEnumerable x = new[] { “hello”, “world”, “how”, “are”, “you” };nIEnumerable enumerable = x.Each(p => p.Length);nnCode:nnpublic static IEnumerable Each(this IEnumerable enumberable, Func action)n{n foreach (var item in enumberable)n {n yield return action(item);n }n}

  10. comment number 10 by: Daniel Hölbling

    You could even extend this to allow for transformations like this:

    IEnumerable<string> x = new[] { “hello”, “world”, “how”, “are”, “you” };
    IEnumerable<int> enumerable = x.Each(p => p.Length);

    Code:

    public static IEnumerable<TOut> Each<T, TOut>(this IEnumerable<T> enumberable, Func<T, TOut> action)
    {
    foreach (var item in enumberable)
    {
    yield return action(item);
    }
    }

  11. comment number 11 by: Snakiej

    which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}

  12. comment number 12 by: Snakiej

    which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}

  13. comment number 13 by: Snakiej

    which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}

  14. comment number 14 by: Snakiej

    which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}

  15. comment number 15 by: Snakiej

    which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}

  16. comment number 16 by: Snakiej

    which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}

  17. comment number 17 by: Snakiej

    which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}

  18. comment number 18 by: Snakiej

    which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}

  19. comment number 19 by: Snakiej

    which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}

  20. comment number 20 by: Snakiej

    which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}

  21. comment number 21 by: Snakiej

    which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}

  22. comment number 22 by: Snakiej

    which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}

  23. comment number 23 by: Snakiej

    which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}

  24. comment number 24 by: Snakiej

    which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}

  25. comment number 25 by: Snakiej

    which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}

  26. comment number 26 by: Snakiej

    which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}

  27. comment number 27 by: Snakiej

    which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}

  28. comment number 28 by: Snakiej

    which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}

  29. comment number 29 by: Snakiej

    which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}

  30. comment number 30 by: Snakiej

    which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}

  31. comment number 31 by: Snakiej

    which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}

  32. comment number 32 by: Snakiej

    which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}

  33. comment number 33 by: Snakiej

    which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}

  34. comment number 34 by: Snakiej

    which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}

  35. comment number 35 by: Snakiej

    which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}

  36. comment number 36 by: Snakiej

    which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}

  37. comment number 37 by: Snakiej

    which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}

  38. comment number 38 by: Snakiej

    which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}

Leave a Reply

Name

Mail (never published)

Website

blog comments powered by Disqus