CSharp - Array.ForEach Enumeration


Arrays are easily enumerated with a foreach statement:

 using System;
 class MainClass{
    public static void Main(string[] args){
         int[] myArray = { 1, 2, 3};
         foreach (int val in myArray)
              Console.WriteLine (val);

You can enumerate using the static Array.ForEach method, defined as follows:

public static void ForEach<T> (T[] array, Action<T> action);

This uses an Action delegate, with this signature:

public delegate void Action<T> (T obj);

The following code rewrite the code Array.ForEach:


using System;
class MainClass//  w w  w.j  a  va  2 s.c  o m
   public static void Main(string[] args)

     Array.ForEach (new[] { 1, 2, 3 }, Console.WriteLine);