Chapter 7: Iterators


Overview

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) {    Console.WriteLine(color); } 

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>(); collection.Push(10); collection.Push(15); collection.Push(20); foreach(int number in collection) {    Console.WriteLine(number); } 

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.




Programming Microsoft Visual C# 2005(c) The Language
Microsoft Visual Basic 2005 BASICS
ISBN: 0619267208
EAN: 2147483647
Year: 2007
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net