CSharp - Array.ForEach Enumeration

Introduction

Arrays are easily enumerated with a foreach statement:

 Code:
 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:

Demo

using System;
class MainClass/*w  w  w  . ja va2 s. co m*/
{
   public static void Main(string[] args)
   {

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

   }
}

Result