By using the foreach statement you can iterate elements of a collection without the need to know the number of elements inside the collection. The foreach statement uses an enumerator. Figure 5-7shows the relationship between the client invoking the foreach method and the collection. The array or collection implements the IEnUlll9rable interface with the GetEnumerator () method. The GetEnumera tor ( ) method returns an enumerator implementing the IEnumerable interface. The interface IEnumerable then is used by the foreach statement to iterate through the collection.
The GetEnumerator () method is defined with the interface IEnumerable. The foreach statement doesn’t really need this interface implemented in the collection class. It’s enough to have a method with the name GetEnumerator () that returns an object implementing the IEnumerator interface.
The foreach statement uses the methods and properties of the IEnumerator interface to iterate all elements in a collection. The properties and methods from this interface are defined in the following table.
The MoveNext () method moves to the next element of the collection and returns true if there’s an element. If the collection does not contain any more elements, the value false is returned.
The property Current returns the element where the cursor is positioned.
The method Reset () repositions the cursor to the beginning of the collection. Many enumerators throw a NotSupportedException.
The C# foreach statement is not resolved to a foreach statement in the IL code. Instead, the C# compiler converts the foreach statement to methods and properties of the IEnumerable interface. Here’s a single foreach statement to iterate all elements in the persons array and to display them person by person:
The foreach statement is resolved to the following code segment. First, the GetEnumerator () method is invoked to get an enumerator for the array. Inside a while loop – as long as MoveNext () returns true – the elements of the array are accessed using the Current property:
C# 1.0 made it easy to iterate through collections by using the foreach statement. With C# 1.0,it was still a lot of work to create an enumerator. C# 2.0 adds the yield statement for creating enumerators easily.
yield return returns one element of a collection and moves the position to the next element, and yield break stops the iteration.
The next example shows the implementation of a simple collection using the yield return statement. The class Hello Collection contains the method GetEnumerator (). The implementation of the GetEnumerator () method contains two yield return statements where the strings Hello and World are returned.
A method or property that contains yield statements is also known as an iterator block. An iterator block must be declared to return an IEnumerator or IEnumerable interface. This block may contain multiple yield return or yield break statements; a return statement is not allowed.
Now it is possible to iterate through the collection using a foreach statement:
public class Program
foreach (string s in helloCollection)
( ~ Console.WriteLine(s);
With an iterator block the compiler generates a yield type, including a state machine, as shown with the following code segment. The yield type implements the properties and methods of the interfaces IEnumerator and IDisposable. In the sample, you can see the yield type as the inner class Enumerat”or. The GetEnumerator () method of the outer class instantiates and returns a new yield type. Within the yield type, the variable state defines the current position of the iteration and is changed e\lery time the method MoveNext () is invoked. MoveNext () encapsulates the code of the iterator block and sets the value of the current variable so that the CUrrent property returns an object depending on the position.
public IEnumerator GetEnumerator()
Enumerator enumerator = new Enumerator();
public class Enumerator
private int state;
private object current;
public Enumerator(int state)
Now, using the yield return statement makes it easy to implement a class that allows iterating through a collection in different ways. The class MusicTi tles allows iterating the titles in a default way with the GetEnumerator () method, in reverse order with the Reverse () method, and to iterate through a subset with the Subset () method:
The client code to iterate through the string array first uses the GetEnumerator () method, which you don’t have to write in your code because this one is used by default. ‘Then the titles are iterated in reverse, and finally a subset is iterated by passing the index and number of items to iterate to the Subset () method:
MusicTitles titles = new MusicTitles();
foreach (string title in titles)
foreach (string title in titles.Reverse())
foreach (string title in
With the yield statement you can also do more complex things, for example, returning an enumerator from yield return.
With the TIcTacToegame, players alternate putting a cross or a circle in one of nine fields. These moves are simulated by the GameMoves class. The methods Cross () and Circle () are the iterator blocks for creating iterator types. The variables cross and circle are set to Cross () and Circle () inside the constructor of the GameMoves class. By setting these fields the methods are not invoked, but set to the iterator types that are defined with the iterator blocks. Within the Cross () iterator block, information about the move is written to the console and the move number is incremented. If the move number is higher than 9, the iteration ends with yield break; otherwise, the enumerator object of the cross yield type is returned with each iteration. The Circle () iterator block is very similar to the Cross () iterator block; it just returns the circle iterator type with each iteration.
From the client program you can use the class GameMoves as follows. The first move is set by setting enumerator to the enumerator type returned by game. cross ().enume racoz .MoveNext invokes one iteration defined with the iterator block that returns the other enumerator, The returned value can be accessed with the Current property and is set to the enumerator variable for the next loop:
GameMoves game = new GameMoves ();
IEnumerator enumerator = game.Cross();
The outcome of this program shows alternating moves until the last move:
Cross, move 0
Circle, move 1
Cross, move 2
Circle, move 3
Cross, move 4
Circle, move 5
Cross, move 6
Circle, move 7
Cross, move 8
You’ve seen the C# notation to create and use simple, multidimensional, and jagged arrays. The Array class is used behind the scenes of C# arrays, and this way you can invoke properties and methods of this class with array variables.
You’ve seen how to sort elements in the array by using the IComparable and IComparer interfaces. The features of the IEnumerable, ICollection, and IList interfaces as implemented with the Array class were described, and finally, you’ve seen the advantages of the yield statement. Moving on, the next chapter focuses on operators and casts. gives you information about delegates and events. Some methods of the Array class use delegates as parameters. Collection class~ give you more flexibility of the size, and there you can also read about other containers such as dictionaries and linked lists .