Traversing an Entire Array with the foreach Statement


Traversing an Entire Array with the foreach Statement

Instead of using the for loop to traverse an entire array, you can apply C#'s foreach statement as a convenient alternative. For example, to print every array element value of the array childbirths declared and defined as

 uint [] childbirths =  { 1340, 3240, 1003, 4987, 3877} ; 

you can write the following foreach statement:

 foreach (uint temp in childbirths) {       Console.WriteLine(temp); } 

to provide the following output:

 1340 3240 1003 4987 3877 

This deserves a closer look. The foreach statement consists of a header and a loop body (see Figure 10.4). The loop body can either be a single statement or a compound statement. The first two words inside the parentheses of the header must consist of a type and an identifier. Together they declare the iteration variable of the foreach statement. In this case, the iteration variable is called temp (for temporary). To the right of the iteration variable, you must write the keyword in, which is followed by the array variable name representing the array to be traversed (here, called childbirths). Observe that the type specified for the iteration variable must be implicitly convertible to the base type of the specified array. The loop body is executed once for every array element in the specified array. The iteration variable can be used in the loop body. During the first execution of the loop body, its value will be equal to the value of the first array element. For every new execution of the loop body, it will hold the value of the next array element. When the foreach statement is terminated, the iteration variable will have traversed the entire collection of array element values.

Figure 10.4. The foreach statement.

Syntax Box 10.4 The foreach Statement

 foreach_statement::= foreach ( <Type> <Iteration_variable_identifier> in <Array_identifier> graphics/ccc.gif)     [ <Statement> | <Compound_statement> ] 

When executing the foreach statement, C# automatically determines the number of iterations. It also assigns the value of each array element to the iteration variable without the need for the programmer to provide explicit indexes. The loop counters, loop conditions, and loop updates needed when applying the conventional for loop are superfluous, providing for an easier, cleaner, and less bug-prone implementation.



The foreach statement can also be used with the collection classes found in the System.Collection namespace of the .NET Framework.



Because the foreach statement hides loop counters, loop updates, and access to array elements under the hood, it is difficult, to follow the detailed mechanics of this type of loop by viewing the code. The for loop in contrast spells out all the gory details. The for loop also provides more flexibility in terms of limiting the access to various parts of the array (for example, when only access to every second or every third array element is needed). For those reasons, the for loop has been used throughout this chapter, even in situations where the foreach statement could have provided a cleaner implementation.


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata © 2008-2017.
If you may any questions please contact us: