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


Iterates over all the elements in a collection and performs the given action, usually given as a lambda.


/// <summary>
///     Applies an Action <typeparamref name="T"/> to all elements
///     of an array.
/// </summary>
/// <typeparam name="T">
///     Type of elements in the array
/// </typeparam>
/// <param name="elements">
///     Array of elements
/// </param>
/// <param name="action">
///     The <see cref="Action{TProperty}"/> to be performed in all
///     elements.
/// </param>
public static void Each<T>(this IEnumerable<T> elements, Action<T> action)
    foreach (var e in elements) action(e);


List<string> l = new List<string>(new[] {"hello", "world"});
l.Each( s => Console.Write(s.ToUpper() );

Author: Ricardo Amores Hernández

Submitted on: 3 mei 2012

Language: C#

Type: System.IEnumerable<T>

Views: 3144