C# Array - ForEach() Method



The C# Array ForEach() method is used to perform the specified action on each element of an array.

The action is a lambda expression that performs an action on the object given to it. The elements of the array are passed individually to the Action<T>.

Syntax

Following is the syntax of the C# Array ForEach() method −

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

Parameters

This method accepts the following parameters −

  • array: The one dimensional zero based array on whose element action to be performed.
  • action: The action to be performed on each element of array.

Return value

This method does not return any value.

Example 1: Display Array Element

This is the basics example that shows how to use ForEach() to display each element in the array −

using System;

class Program {
   static void Main() {
      int[] numbers = { 1, 2, 3, 4, 5 };

      // Display each number in the array
      Array.ForEach(numbers, number => Console.WriteLine(number));
   }
}

Output

Following is the output −

1
2
3
4
5

Example 2: Multiply Each Element By 2

Let's create an example that displays each element multiplied by 2. We use the ForEach() method to perform the calculation of the each element −

using System;

class Program {
   static void Main() {
      int[] numbers = { 1, 2, 3, 4, 5 };

      // mutliplay each number by 2 and print them
      Array.ForEach(numbers, number => Console.WriteLine(number * 2));
   }
}

Output

Following is the output −

2
4
6
8
10

Example 3: Modify Object Property

Let's see another example of the ForEach() method, Here we will manipulate the object's property of the person −

using System;

class Person {
   public string Name {
      get;
      set;
   }
   public int Age {
      get;
      set;
   }
}

class Program {
   static void Main() {
      Person[] people = {
         new Person {
            Name = "Jessica", Age = 25
         },
         new Person {
            Name = "Luna", Age = 27
         },
         new Person {
            Name = "Amber", Age = 33
         }
      };

      // Increment each person's age by 2
      Array.ForEach(people, person => person.Age += 2);

      // Display updated ages
      Array.ForEach(people, person => Console.WriteLine($"{person.Name}: {person.Age}"));
   }
}

Output

Following is the output −

Jessica: 27
Luna: 29
Amber: 35

Example 4: Calculate the Square of Each Element

The below example uses the ForEach() method to perform an action to calculate the square of each element in the array −

using System;

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

      // set a delegate for the ShowSquares method
      Action <int> action = new Action <int> (ShowSquares);

      Array.ForEach(intArray, action);
   }
   private static void ShowSquares(int val) {
      Console.WriteLine("{0:d} squared = {1:d}", val, val * val);
   }
}

Output

Following is the output −

2 squared = 4
3 squared = 9
4 squared = 16
csharp_array_class.htm
Advertisements