Iterators in C#
When it is defined, an iterator is a language construct based on similar features in research languages such as CLU, Sather and Icon. Simply put, iterators make it easy for types to declare how the for each statement will iterate over their elements.
The necessity of Iterators
If classes want to support iteration using the foreach loop construct, they must implement the "enumerator pattern". For example, the foreach loop construct on the left is expanded by the compiler into the while loop construct on the right:
List list = ...; foreach(object obj in list) { DoSomething(obj); }
Enumerator e = list.GetEnumerator(); while(e.MoveNext()) { object obj = e.Current; DoSomething(obj);
Discern that the List data structure-the instance being iterated-must support the GetEnumerator function in order for the foreach loop to work. Now that the List data structure is created, the GetEnumerator function must be implemented, which then returns a ListEnumerator object:
public class List { internal object[] elements; internal int count; public ListEnumerator GetEnumerator() { return new ListEnumerator(this); } }
The ListEnumerator object that was created must not only implement the Current property and the MoveNext method, but it must also maintain its internal state so that your program can move to the next item each time around the loop. Implementing this enumerator pattern could require a great deal of effort and code on the developer's part, C# will include a new construct that will make it easier for a class to dictate how the foreach loop will iterate over its contents. This internal state machine can be simple for the List data structure, but for data structures that require recursive traversal, such as binary trees, the state machine can be quite complex.
Iterator - definition
It is defined similarly to a function using the foreach keyword, followed by an open and closed parenthesis pair. An iterator is the logical counterpart of the foreach loop construct. In the following example, your program will declare an iterator for the List type. The return type of the iterator is determined by the user, but since the List class stores an object type internally, the return type of the following iterator example will be an object:
public class List { internal object[] elements; internal int count; public object foreach() { } }
Once the enumerator pattern is implemented, your program needs to maintain an internal state machine in order to keep track of where the program is in the data structure. Iterators have built-in state machines. Using the new yield keyword, your program can return values back to the foreach statement that called the iterator. The next time the foreach statement loops and calls the iterator again, the iterator begins its execution where the previous yield statement left off. In the following example, your program yields three string styles:
public class List { internal object[] elements; internal int count; public string foreach() { yield "microsoft"; yield "corporation"; yield "developer division"; } }
The foreach loop that calls this iterator will execute three times In the following example. Each time receiving the strings in the order specified by the previous three yield declarations:
List list = new List(); foreach(string s in list) { Console.WriteLine(s); }
If you want the program to implement the iterator to traverse the elements in the list, you would modify the iterator to step across the array of elements using a foreach loop, yielding each item in the array in every iteration:
public class List { internal object[] elements; internal int count; public object foreach() { foreach(object o in elements) { yield o; } } }
The working procedure of Iterators
Iterators hold the messy task of implementing the enumerator pattern on behalf of your program. Rather than having to create the classes and build the state machine, the C# compiler translates the code you have written in your iterator into the appropriate classes and code using the enumerator pattern. In so doing, iterators provide a significant increase in developer efficiency.
Have a nice time!