Skip to content

Instantly share code, notes, and snippets.

@AlexArchive
Created July 20, 2013 00:17
Show Gist options
  • Save AlexArchive/6043251 to your computer and use it in GitHub Desktop.
Save AlexArchive/6043251 to your computer and use it in GitHub Desktop.
ForEach Extension Method
public static class AutomaticIterator
{
public static void ForEach<T>(this IEnumerable<T> iterable, Action<T> action)
{
var iterator = iterable.GetEnumerator();
using (iterator)
{
while (iterator.MoveNext())
{
action(iterator.Current);
}
}
}
}
...
var collection = new[] { 10, 20, 30 };
collection.ForEach(Console.WriteLine);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment