Last active
September 27, 2015 02:48
-
-
Save AnthonyMastrean/1199470 to your computer and use it in GitHub Desktop.
Reverse foreach structure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!