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

ForEach

Foreach inline for the IEnumerable<T>.

Source

public static void ForEach<T>(this IEnumerable<T> list, Action<T> action)
{
    foreach (var item in list)
    {
        action.Invoke(item);
    }
}

Example

IEnumerable<User> users = GetUsers();

users.ForEach(x => x.DoSomething());

users.ForEach(x => 
{
       x.DoSomething();
       x.MagicNumber = 1;
});

Author: Leandro Boffi

Submitted on: 16 jun. 2010

Language: C#

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

Views: 5343