foreach Enumeration of Arrays


Many collections in the .Net environment service foreach enumeration and arrays are no exception. The foreach keyword is a context sensitive keyword that relies upon certain .Net interfaces (such as IEnumerable and IEnumerator). The underlying collection class implements these interfaces and the foreach keyword is then able to perform its tasks. Thus, it is because arrays are actually collection classes implementing these interfaces that foreach may be applied to them.

The foreach keyword as applied to a collection resembles the universal quantifier in predicate logic. It cycles through a collection (array in this case) delivering each element of the collection to a loop. The syntax of a foreach loop is shown below.

foreach (type variable-name in collection) statement;

The variable-name is a program defined iteration variable that receives the values of individual elements of the collection. The type matches the enumeration type of the collection (as defined by the interface IEnumerable). An important fact is that the variable is constant with regards to the iteration process; hence, it is not possible to change the contents of the collection by assigning to the variable.

The following simple program demonstrates the use of foreach enumeration on an array.

// ArrayForEach - Using foreach Enumeration on an Array

using System;

class Program
{
    static void Main()
    {
        int[] ai = new int[10];

        for (int i = 0; i < 10; i++)
            ai[i] = 10 - i;

        foreach (int i in ai)
            Console.WriteLine(i);
    }
}

The output of this program is shown below.

10
9
8
7
6
5
4
3
2
1

The order of enumeration for an array is in ascending order of the elements. It is possible to break out of a foreach loop at a specified location, as the following program demonstrates.

// ArrayForEach2 - Breaking from a foreach loop

using System;

class Program
{
    static void Main()
    {
        int[] ai = new int[10];

        for (int i = 0; i < 10; i++)
            ai[i] = i;

        int sum=0;

        foreach (int i in ai)
        {
            if (i == 5) break;
            sum += i;
        }

        Console.WriteLine("The sum of 0...4 = " + sum);
    }
}

The output of this program is as follows.

The sum of 0...4 = 10

In the above example, the array looks like the following.

0
1
2
3
4
5
6
7
8
9

The given foreach statement starts at the first element (0 in this case) and proceeds to element 5; whereby a break statement is issued. The summation is thus from 0...4.

Using foreach on Multidimensional Arrays

A foreach loop may be applied to a multidimensional array. The array is stored in row order and the foreach delivers elements in that order. For example, consider the following enumeration of a two dimensional array.

// ArrayForEach3 - A foreach loop on a 2D array

using System;

class Program
{
    static void Main()
    {
        int[,] ai = new int[2,3];

        for (int i = 0; i < 2; i++)
            for (int j = 0; j < 3; j++)
              ai[i,j] = j + i * 3;

        foreach (int i in ai)
        {
            Console.WriteLine(i);
        }
    }
}

The output of the program is as follows.

0
1
2
3
4
5