TheDeveloperBlog.com


C# Array.ForEach Method Examples

Array.ForEach. A method, Array.ForEach loops over every element. It calls a method on each element in an array. It is a declarative syntax form. It simplifies certain code patterns. No loop construct is needed.


Example. Consider this ForEach example. We call Array.Foreach on an int array. We cannot modify values within the array, but we can act upon them. Here we call Console.WriteLine with each integer element.

Action: The Action type (the second argument in ForEach) returns no value: it is void. It cannot directly modify elements.

Based on:

.NET 4.5

C# program that uses ForEach, WriteLine

using System;

class Program
{
    static void Main()
    {
	int[] items = { 10, 100, 1000 };
	// Display elements with ForEach.
	Array.ForEach(items, element => Console.WriteLine(
	    "Element is " + element));
    }
}

Output

Element is 10
Element is 100
Element is 1000


Example 2. ForEach uses an Action type as its second argument. We can only modify reference types through this method. We can call a certain method on each element but cannot change a value type element.

Action

Next: This example demonstrates, with a jagged array, how we can modify the reference types from ForEach.

Jagged Arrays
C# program that uses Array.ForEach method

using System;

class Program
{
    static void Main()
    {
	// Allocate a jagged array and put three subarrays into it.
	int[][] array = new int[3][];
	array[0] = new int[2];
	array[1] = new int[3];
	array[2] = new int[4];
	// Use the ForEach method to modify each subarray's first element.
	// ... Because the closure variable is an array reference,
	//     you can change it.
	Array.ForEach(array, subarray => subarray[0]++);
	foreach (int[] subarray in array)
	{
	    foreach (int i in subarray)
	    {
		Console.Write(i);
	    }
	    Console.WriteLine();
	}
	// Apply the same routine with ForEach again.
	Array.ForEach(array, subarray => subarray[0]++);
	foreach (int[] subarray in array)
	{
	    foreach (int i in subarray)
	    {
		Console.Write(i);
	    }
	    Console.WriteLine();
	}
    }
}

Output

10
100
1000
20
200
2000

This example program shows how to allocate a jagged array and then apply the Array.ForEach method with two arguments. The arguments are the array itself, and a lambda expression that represents an Action type instance.

Lambda: The left part before the => token is the argument name, and the right part is the expression taken upon the argument.

Lambdas

Info: In this example, the first array element in each subarray is modified. Usually, with ForEach, this style of code is not helpful.


Discussion. This method has limitations. The Action delegate receives a formal parameter list. These arguments are copied by value. When we copy an element of an array as a parameter, it becomes a scalar variable.

So with ForEach, we cannot directly modify array elements. The change is not reflected in the array. If we modify something to a reference points, though, that change is reflected.

ToArray: A LINQ query, with the ToArray method, is an easier way to create a modified array.

ToArrayLINQ

Thus: This method is not a "map" method. It is a loop abstraction, but not a good way to mutate every array element.

Map, Python

Summary. Array.ForEach is a declarative loop. By applying this method, we can invoke a method on each array element. Or we can modify something based on a reference (class) element. This is not a map method. We cannot modify value type elements.