using System; using System.Collections.Generic; using System.Linq; namespace EachExtensionExample { public static class EachIteratorExtension { public static void Each(this IEnumerable source, Action iterator) { foreach (TSource item in source) iterator(item); } } internal class Program { private static void Main() { string[] collection = { "Hello", "World" }; collection.Each(item => Console.WriteLine(item)); Console.WriteLine(); //of meerdere lijnen collection.Each(item => { Console.WriteLine(item); Console.WriteLine(item.ToUpper()); }); Console.WriteLine(); //maar hetzelfde werkt met zowat alle .NET collecties //enige vereiste is dat ze IEnumerable implementeren var dates = new List(); Enumerable.Range(0, 7).Each(day => dates.Add(DateTime.Now.AddDays(day))); dates.Each(d => Console.WriteLine(d.DayOfWeek)); var dates2 = new List() { DateTime.Now }; var nestedList = new List> { dates, dates2 }; nestedList.Each(list => Console.WriteLine("Dates in list: {0}", list.Count) ); Console.ReadLine(); } } }