Skip to content

Instantly share code, notes, and snippets.

@AnthonyMastrean
Last active September 27, 2015 02:48
Show Gist options
  • Save AnthonyMastrean/1199470 to your computer and use it in GitHub Desktop.
Save AnthonyMastrean/1199470 to your computer and use it in GitHub Desktop.
Reverse foreach structure
namespace System
{
public static class ReverseForeach
{
public static void ForEach<T>(this Action<T> action, IEnumerable<T> enumerable)
{
foreach(var item in enumerable)
{
action(item);
}
}
}
}
[TestClass]
public class When_iterating_over_an_enumerable
{
private readonly Action<int> _print = x => Console.WriteLine(x.ToString());
[TestMethod]
public void It_should_print_all_digits()
{
_print.ForEach(Enumerable.Range(0, 10));
}
}
@AnthonyMastrean
Copy link
Author

I was reading Justin Bozonier's Coffeescript post and was intrigued by the syntax of the foreach loop. I decided to switch it around for C#. Not sure I like reading it like that. Although, I might simply not be used to it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment