Information Technology Reference
In-Depth Information
Using the IEnumerator Interface
This section will start by looking at the first in the preceding list: the non-generic interface
form. This form of enumerator is a class that implements the IEnumerator interface. It is called
non-generic because it does not use C# generics.
The IEnumerator interface contains three function members: Current , MoveNext , and
Reset .
￿ Current is a property that returns the item at the current position in the sequence.
-
It is a read-only property.
It returns an object of type object , so an object of any type can be returned.
-
￿ MoveNext is a method that advances the enumerator's position to the next item in the
collection. It also returns a Boolean value, indicating whether the new position is a valid
position or is beyond the end of the sequence.
-
If the new position is valid, the method returns true .
If the new position is not valid (i.e., it's at the end), the method returns false .
-
-
The initial position of the enumerator is before the first item in the sequence.
MoveNext must be called before the first access of Current .
￿ Reset is a method that resets the position to the initial state.
Figure 20-2 illustrates a collection of three items, which are shown on the left of the figure,
and its enumerator, which is shown on the right. In the figure, the enumerator is an instance of
a class called ArrEnumerator .
Figure 20-2. The enumerator for a small collection
Search WWH ::




Custom Search