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

ForEach

Shortcut for foreach

Source

using System;
using System.Collections.Generic;

namespace Extensions
{
    static class Extensions
    {
        public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
        {
            foreach (var item in source)
                action(item);
        }
    }
}

Example

List<string> names = new List<string> { "beatle", "assassin", "chrisis", "erique", "fanlan" };
names.ForEach(name => Console.WriteLine(name));

Author: Jeroen de Zeeuw, Rob van Lanen

Submitted on: 21 mrt. 2008

Language: C#

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

Views: 13182