Enumerators

Enumerators

The next chapter pertains to enumerators, a topic that is a natural extension of this chapter. Enumerators are typically used to iterate collections, which have been the focus of the last two chapters. Enumerators are implemented and typically exposed from an enumerable object. Enumerable objects implement the IEnumerable interface. The IEnumerable.GetEnumerator method returns an enumerator object.

The foreach statement is the most visible expression of enumeration in a C# program. The target of the foreach statement is not any object—it has to be an enumerable object.

The next chapter documents the enumerator pattern, including how to implement an enumerator and enumerable object.