Information Technology Reference
In-Depth Information
The Generic Enumeration Interfaces
The third form of the enumerator uses the generic interfaces
IEnumerable<T>
and
IEnumerator<T>
.
They are called generic because they use C# generics. Using them is very similar to using the non-
generic forms. Essentially, the differences between the two are the following:
With the non-generic interface form
-The
GetEnumerator
method of interface
IEnumerable
returns an enumerator class
instance that implements
IEnumerator
.
The class implementing
IEnumerator
implements property
Current
, which returns
an
object
.
-
With the generic interface form
-The
GetEnumerator
method of interface
IEnumerable<T>
returns an enumerator class
instance that implements
IEnumerator<T>
.
The class implementing
IEnumerator<T>
implements property
Current
, which returns
a derived type.
-
Notice that, with the
generic interfaces
, the items you receive from the enumeration are a
specific derived type, whereas with the non-generic interfaces, you get back the more “generic”
object
from the enumerations.