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

Each

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

Source

/// <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);
}

Example

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

Author: Ricardo Amores Hernández

Submitted on: 3 mei 2012

Language: C#

Type: System.IEnumerable<T>

Views: 2846