Array - ForEach

A T[] extension method that applies an operation to all items in this collection.

Try it

public static void Main()
{
    int[] intArray = new int[] { 2, 3, 4 };

    Action<int> action = new Action<int>(ShowSquares);

    // C# Extension Method: Array - ForEach
    intArray.ForEach(action);
}

private static void ShowSquares(int val)
{
    Console.WriteLine("{0:d}^2 = {1:d}", val, val * val);
}

View Source
using System;

public static partial class Extensions
{
    /// <summary>
    ///     A T[] extension method that applies an operation to all items in this collection.
    /// </summary>
    /// <typeparam name="T">Generic type parameter.</typeparam>
    /// <param name="array">The array to act on.</param>
    /// <param name="action">The action.</param>
    public static void ForEach<T>(this T[] array, Action<T> action)
    {
        Array.ForEach(array, action);
    }
}