How to loop through a collection that supports IEnumerable?

Viewed 204008

How to loop through a collection that supports IEnumerable?

5 Answers

You might also try using extensions if you like short code:

namespace MyCompany.Extensions
{
    public static class LinqExtensions
    {
        public static void ForEach<TSource>(this IEnumerable<TSource> source, Action<TSource> actor) { foreach (var x in source) { actor(x); } }
    }
}

This will generate some overhead, for the sake of having stuff inline.

collection.Where(item => item.IsReady).ForEach(item => item.Start());
Related