IEnumerable<T>.Each as C# Extension Method
February 11th, 2009 . by Daniel HölblingA 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



[...] 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 [...]
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.
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;
}
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}
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}
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}
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}
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}
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}
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);
}
}
which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}
which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}
which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}
which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}
which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}
which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}
which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}
which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}
which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}
which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}
which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}
which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}
which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}
which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}
which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}
which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}
which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}
which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}
which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}
which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}
which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}
which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}
which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}
which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}
which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}
which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}
which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}
which can be reduced to:nnttpublic static IEnumerable Each(this IEnumerable enumberable, Func action)ntt{ntttreturn enumberable.Select(action);ntt}