Information Technology Reference
In-Depth Information
Figure 20-5.
Comparing interface-based and non-interface-based enumerators
One possible problem with the non-interface enumerator implementation is that types
from other assemblies might expect enumeration to be implemented using the interface
method. If these objects attempt to get an enumeration of your class objects using the interface
conventions, they will not be able to find them.
To solve this problem, you can implement both forms in the same classes. That is, you can
create implementations for
Current
,
MoveNext
,
Reset
, and
GetEnumerator
at the class level, and
also create
explicit
interface implementations for them. With both sets of implementations, the
type-safe, more efficient implementation will be called by
foreach
and other constructs that
can use the non-interface implementations, while the other constructs will call the explicit
interface implementations.