Is there a way to call a method on every object in a List – e.g.
Instead of
List<MyClass> items = setup();
foreach (MyClass item in items)
item.SomeMethod();
You could do something like
foreach(items).SomeMethod();
Anything built in, extension methods, or am I just being too damn lazy?
Yes, on
List<T>, there is:The oddity is that this is only available on
List<T>, notIList<T>orIEnumerable<T>To fix this, I like to add the following extension method:
Then, you can use it on ANYTHING that implements
IEnumerable<T>… not justList<T>