-
Notifications
You must be signed in to change notification settings - Fork 2
Home
Aaron edited this page May 18, 2019
·
7 revisions
Welcome to the utilities wiki! This is a work in progress. Feel free to contribute.
List<T>
implements ForEach()
, which is a lot of fun. But we need other places to use it. Here you go:
public static void ForEach<T>(this IEnumerable<T> collection, Action<T> action)
{
foreach (T obj in collection)
action(obj);
}
Now if you refactor your code that uses List<T>
to use IEnumerable<T>
, you don't have to rewrite all your loops. For example:
var list = GetList();
list.ForEach(i => i.Counter++);
This will still work:
var list = GetList().AsEnumerable();
list.ForEach(i => i.Counter++);