Skip to content

Instantly share code, notes, and snippets.

@dgg
Last active February 13, 2016 20:28
Show Gist options
  • Select an option

  • Save dgg/8ccc609ce8dad71efd28 to your computer and use it in GitHub Desktop.

Select an option

Save dgg/8ccc609ce8dad71efd28 to your computer and use it in GitHub Desktop.
burned-by-laziness-again
// "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);
// 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