M
Michael C
Linq defines all sorts of useful extension methods but as far as I can see
it doesn't have a "do something to every item in a collection", eg to add 1
to SomeProperty for every item in my collection:
MyCollection.ForEach(i => i.SomeProperty += 1);
The array has a ForEach method but nothing else does. I've defined my own
but am wondering if I'm missing something that is already existing.
Cheers,
Michael
public static void ForEach<T>(this IEnumerable<T> Items, Action<T>
action)
{
foreach (T item in Items)
{
action(item);
}
}
public static void ForEach(this IEnumerable Items, Action<object>
action)
{
foreach (object item in Items)
{
action(item);
}
}
it doesn't have a "do something to every item in a collection", eg to add 1
to SomeProperty for every item in my collection:
MyCollection.ForEach(i => i.SomeProperty += 1);
The array has a ForEach method but nothing else does. I've defined my own
but am wondering if I'm missing something that is already existing.
Cheers,
Michael
public static void ForEach<T>(this IEnumerable<T> Items, Action<T>
action)
{
foreach (T item in Items)
{
action(item);
}
}
public static void ForEach(this IEnumerable Items, Action<object>
action)
{
foreach (object item in Items)
{
action(item);
}
}