Chapter 7: Iterators

Chapter 7: Iterators


Stacks, queues, dictionaries, dynamic arrays, normal arrays, and various other collections were introduced in previous chapters. Collections are assemblages of related types. There could be a dynamic array of Employee types, which would include HourlyEmployee, ExemptEmployee, and CommissionedEmployee instances. There could be a queue of bank requests, such as DepositTransaction, WithdrawalTransaction, and InterestTransaction types. Inheritance relates these types at compile time, whereas polymorphism binds them at run time. Collections are also assemblages of similar items. Enumerating the elements of a collection is a recurrent and valuable behavior. Enumeration iterates the items in a collection. A report that lists new employees would need to enumerate the employee collection. Bank statements require enumerating transactions of a particular bank client.

Enumeration is a valuable tool to almost every developer, encouraging some standardization. Enumerable nongeneric types implement the IEnumerator and IEnumerable interfaces, whereas generic types implement the IEnumerator<T> and IEnumerable<T> generic interfaces. IEnumerable interfaces return unique enumerator objects to clients. An enumerator object implements the IEnumerator interface, which defines the behavior of enumeration. Collections are enumerable objects, which implement one of the IEnumerable interfaces and adhere to the enumeration pattern.

Collections are usually enumerated in a loop. Most collections offer collection-specific interfaces to enumerate elements. The ArrayList type has indexes. The Stack has the Pop method. Hashtables have the Keys and Values properties. Collections also offer a standardized approach to enumeration with the IEnumerator interface. Loops are often harbingers of subtle bugs. The foreach statement is a compiler-generated loop that uses IEnumerator objects to iterate error-free enumerable collections. This reduces the code required to iterate a collection and makes developer code more robust.

The following is a straightforward foreach loop. The colors array is an array of strings. Arrays are collections and are therefore enumerable. Instead of writing a for loop, with indexes and a counter to manage, the foreach loop keeps everything simple. No indexes. No counters. The foreach loop automatically enumerates each element of the collection.

string [] colors={"red", "green", "blue"};
foreach(string color in colors) {

Here is another example of a foreach loop. This code manipulates a Stack collection—a more complex structure than a simple array. However, stacks are also collections, meaning that the foreach statement is available.

Stack<int> collection=new Stack<int>();
foreach(int number in collection) {

Implementing the IEnumerator interface in an enumerable object can be challenging—especially for complex data structures. Iterators have been introduced for this reason. Iterators provide a standardized implementation of the enumerator pattern and prevent individual developers from re-creating the same wheel.