While you're correct for IEnumerator.Current
's documentation, IEnumerator<T>.Current
's documentation states that the property is undefined for such a scenario. With your iterator, it returns "2". List<T>
's enumerator returns default(T)
, and T[]
's throws an exception. These are all valid implementations, since it's undefined.
Current is undefined under any of the following conditions:
- The enumerator is positioned before the first element in the collection, immediately after the enumerator is created. MoveNext must be called to advance the enumerator to the first element of the collection before reading the value of Current.
- The last call to MoveNext returned false, which indicates the end of the collection.
- The enumerator is invalidated due to changes made in the collection, such as adding, modifying, or deleting elements.
It's worth noting that, even though it implements the interface, the code generated from the yield return
does not correctly implement IEnumerator
, since it continues to return 2
in this case:
IEnumerator enumerator = GetCounter().GetEnumerator();
for (int i = 0; i < 10; i++)
{
enumerator.MoveNext();
Console.WriteLine (enumerator.Current);
}
(for comparison, List<T>
does it correctly: if you get IEnumerator.Current
after the end, it throws an exception, and if you call IEnumerator<T>.Current
after the end, it returns default(T)
)