Comparing IEnumerable and IEnumerator
The IEnumerable
and IEnumerator
interfaces can both be used for iteration but in different ways. Let's understand each in brief.
An object of the IEnumerable
type will know how to traverse the collection that it holds, regardless of what its internal structure is like. There is one method that makes up an enumerable: GetEnumerator()
. It returns as an instance of a class that implements the IEnumerable
interface. Iteration is normally carried out using a foreach
loop. Iterations of an enumerable are carried out using a foreach
loop. However, an enumerable does not remember its location when iterating.
Objects of the Ienumerator
type declare two methods: MoveNext()
and Reset()
. There is one property called Current
that gets the current item in the list that's being enumerated. The MoveNext()
method moves to the next record in a collection and returns a Boolean value indicating the end of the collection. Reset()
will reset the position...