Every type that implements the IEnumerable
interface should implement a method GetEnumerator
which returns a type that implements the IEnumerator
interface. Then having an enumerator you can use the foreach
statement, which in its essence implements the iterator
pattern, to traverse the elements of a collection of your custom type.
For further clarification please take a look to the following links:
So in order to answer to your question, I have to say that we shouldn't try to compare the IEnumerable
and IEnumerator
. They both are used together, when we want to use the iterator pattern for a custom type.