Last active
February 13, 2016 20:28
-
-
Save dgg/8ccc609ce8dad71efd28 to your computer and use it in GitHub Desktop.
burned-by-laziness-again
This file contains hidden or 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
| // "proper" | |
| foreach (var item in collection) | |
| { | |
| doSomething(item); | |
| } | |
| // compact | |
| foreach (var item in collection) doSomething(item); | |
| // more compact | |
| collection.ForEach(item => doSomething(item)); | |
| // can't make it shorter | |
| collection.ForEach(doSomething); |
This file contains hidden or 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
| // 100% working | |
| public static void ForEach<T>(this IEnumerable<T> collection, Action<T> action) | |
| { | |
| foreach (var element in collection) | |
| { | |
| action(element); | |
| } | |
| } | |
| // 99.9% clever | |
| public static IEnumerable<T> ForEach<T>(this IEnumerable<T> collection, Action<T> action) | |
| { | |
| foreach (var element in collection) | |
| { | |
| action(element); | |
| yield return element; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment