HomeSearch

C# Array.ForEach: Use Lambda on Every Element

This C# example program uses the Array.ForEach method on an int array. It uses a lambda on every element.
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.Array
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.Int Array

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

Action
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.

Here: This example program shows how to allocate a jagged array and then apply the Array.ForEach method with two arguments.

Jagged Arrays

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: The first array element in each subarray is modified. Usually, with ForEach, this style of code is not helpful.

C# program that uses Array.ForEach method using System; class Program { static void Main() { // Allocate a jagged array and put 3 subarrays into it. int[][] array = new int[3][]; array[0] = new int[2]; array[1] = new int[3]; array[2] = new int[4]; // Use ForEach 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
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.

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.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls