3
I tried to see the code of both interfaces, but the only difference I see is the IEnumerable<T>
has the method IEnumerator<T> GetEnumerator();
, and that at the interface IEnumerator<T>
the Current
returns the "T" instead of an Object...
Would that be just to prevent the foreach from making a Unboxing after? or not?
And another question, usually you do another class even to implement the interface IEnumerator
and implements all methods Movenext, Reset, Current, etc... or just implement the IEnumerable<T>
and return a Getenumerator() from the List is enough?
Example:
public IEnumerator<Error> GetEnumerator()
{
return this._errors.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return this._errors.GetEnumerator();
}
GetEnumerator()
is an array class method that returns to us a class that implements the IEnumerator<T>
even?
The standard and' implement the second method so:
return GetEnumerator();
in order to avoid duplication of code.– dcastro