ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

Each<T>

iterates through an IEnumerable<T> and applies an Action

Source

public static void Each<T>(this IEnumerable<T> items, Action<T> action)
{
    if (items == null) return;

    var cached = items;

    foreach (var item in cached)
        action(item);
}

Example

class Item
{
    void DoSomething();
}

var items = new List<Item>();
// populate items
items.Each(item => item.DoSomething());

/* OR */

var items = new List<Item>();
// populate items
items.Each(DoSomething);

void DoSomething(Item item)
{
    // do something to item
}

Author: Jon Erickson

Submitted on: 14 okt. 2010

Language: C#

Type: System.Collections.Generic.IEnumerable<T>

Views: 10486