Information Technology Reference
In-Depth Information
The IEnumerator<T> Interface
The IEnumerator<T> interface uses generics to return an actual derived type, rather than an
object of type object .
The IEnumerator<T> interface derives from two other interfaces: the non-generic
IEnumerator interface and the IDisposable interface. It must therefore implement their
members.
You have already seen the non-generic IEnumerator interface and its three members.
￿
￿The IDisposable interface has a single, void , parameterless method called Dispose ,
which can be used to free unmanaged resources being held by the class.
￿The IEnumerator<T> interface itself has a single method, Current , which returns an item
of a derived type— not an item of type object .
￿Since both IEnumerator<T> and IEnumerator have a member named Current , you should
explicitly implement the IEnumerator version, and implement the generic version in the
class itself, as shown in Figure 20-6.
Figure 20-6 illustrates the implementation of the interface.
Figure 20-6. Implementing the IEnumerator<T> interface
The declaration of the class implementing the interface should look something like the
pattern in the following code, where T is the type returned by the enumerator.
Search WWH ::




Custom Search