Branching and Looping in C#, Part 2
(Page 1 of 6 )
In the first part of this article we discussed if, switch and for statements. In part 2 we will continue our looping statements (while, do/while and foreach) and we will discuss jump statements (break, continue, goto and return).
foreach statement
Visual Basic programmers have been accustomed to using the For Each loop for several years to iterate through an array or a collection. C# features a new loop construction that is easy to use and similar to VB's For Each loop; it's the foreach loop. As you know, you can use a for statement to loop over an array or collection, but there are some problems that you may encounter that produce logic errors.
For example, you may need to iterate through all the elements in an array using the for statement. If you declared the variable of the loop to 1, the first element in the array will not be accessed. Another problem that can occur is that you may forget and step 2 instead of one, or even worse (as in our last example in Part 1), you use a decrement step -- the list of potential problems is pretty long, in fact.
The C# foreach loop is used to iterate through each and every element in an array or collection, and you don't have to worry about anything. Although it's little slower than the for loop, the C# compiler optimizes foreach if used with arrays. Here is the syntax of the foreach statement:
foreach(type declaration in collection)
{
statement 1;
statement 2;
statement 3;
}
Let's take an example:
using System;
namespace MyCompany
{
public class Loops
{
static void Main(string[] args)
{
int[] myArray = {10, 20, 30, 40};
foreach(int temp in myArray)
{
Console.WriteLine(temp);
}
Console.ReadLine();
}
}
}
Compile and run the above code and you will get the following result.

Note that we have declared a variable of type int inside the foreach loop to hold the current array's element, and this variable is accessible in the statement block. The foreach loop is intelligent, so for each iteration, it updates its local variable with the new element. With foreach you will not manually increase a counter and set a loop, because it will do it for you. The foreach loop comes with a cost also: you can't assign a new value to the array's elements, because they are read-only inside the foreach loop (via the local variable that we use). Modify the last example so it will look like this:
foreach(int temp in myArray)
{
Console.WriteLine(temp);
temp = 100;
}
When you compile the application you will get the following error:
"Cannot assign to 'temp' because it is read-only."
If the element is not a primitive type, you can call a method of the object to modify it or update its state. You can use the foreach statement with the built-in collection classes, or with your handmade collection classes, but in this case you need to implement some interfaces that give foreach the methods to use in iteration through the objects. Anyway, this is beyond the scope of this article, so I will defer that until we discuss Data Structure and Collections with C#.
Next: The while Loop >>
More C# Articles
More By Michael Youssef