www.gibmonks.com

Table of Contents

Previous Page
Next Page

Recipe 5.16. Performing an Action on Each Element in an Array or List<T>

Problem

You need an easy way to iterate over all the elements in an Array or List<T>, performing an operation on each element as you go.

Solution

Use the ForEach method of the Array or List<T> classes:

	// Create and populate a List of Data objects.
	List<Data> numbers = new List<Data>();
	numbers.Add(new Data(1));
	numbers.Add(new Data(2));
	numbers.Add(new Data(3));
	numbers.Add(new Data(4));

	// Display them.
	foreach (Data d in numbers)
	    Console.WriteLine(d.val);

	// Add 2 to all Data.val integer values.
	
	numbers.ForEach(delegate(Data obj)
	{
	    obj.val += 2;
	});

	// Display them.
	foreach (Data d in numbers)
	    Console.WriteLine(d.val);

	// Total val integer values in all Data objects in the List.
	int total = 0;
	
	numbers.ForEach(delegate(Data obj)
	{
	    total += obj.val;
	});

	// Display total.
	Console.WriteLine("Total: " + total);

This code outputs the following:

	1
	2
	3
	4
	3
	4
	5
	6
	Total: 18

The Data class is defined as follows:

	public class Data
	{
	    public Data(int v)
	    {
	        val = v;
	    }

	    public int val = 0;
	}

Discussion

The ForEach method of the Array and List<T> collections allows you to easily perform an action on every element within these collections. This is accomplished through the use of the Action<T> delegate, which is passed in as a parameter to the ForEach method:

	public void ForEach (Action<T> action)

The action parameter is a delegate of type Action<T> that contains the code that will be invoked for each element of the collection.

The ForEach method basically consists of a loop that iterates over each element in the collection. Within this loop, a call to the action delegate is invoked. Processing continues on to each element in the collection until the last element is finished processing. When this occurs, the ForEach method is finished and returns to the calling method.

This recipe uses the ForEach method of a List<T> object in two different ways. The first is to actually modify the values of each element of the List<T> object:

	// Add 2 to all Data.val integer values.
	numbers.ForEach(delegate(Data obj)
	{
	    obj.val += 2;
	});

This call to ForEach will iterate over each Data element within the numbers List<Data> object. On every iteration, the value val contained in the current Data object obj has its value incremented by two.

The second way is to collect a total of all the values val contained in each Data object obj in the numbers List<Data> object:

	// Total val integer values in all Data objects in the List.
	int total = 0;
	numbers.ForEach(delegate(Data obj)
	{
	    total += obj.val;
	});

This code uses the total variable to build a running total of the values contained in each element. In this instance you do not modify any values in any of the Data objects; instead, you examine each Data object and record information about its value.

See Also

See the "Array Class," "List<T> Class," and "ForEach Method" topics in the MSDN documentation.


Previous Page
Next Page