Information Technology Reference
In-Depth Information
Figure 20-1. Overview of enumerators and enumerables
The foreach construct is designed to work with enumerables. As long as the object it is
given to iterate over is an enumerable type, such as an array, it will perform the following
actions:
Getting the object's enumerator by calling the GetEnumerator method
￿
￿
Requesting each item from the enumerator and making it available to your code as the
iteration variable , which your code can read, but not change
Must be enumerable
foreach( Type VarName in EnumerableObject )
{
...
}
Types of Enumerators
There are three variations on enumerators. They all work essentially the same way, with only
slight differences. I will discuss all three types. You can implement enumerators using
￿The IEnumerator / IEnumerable interfaces—called the non-generic interface form
￿The IEnumerator<T> / IEnumerable<T> interfaces—called the generic interface form
￿No interfaces
Search WWH ::




Custom Search