Question

Consider the documentation for IEnumerator.Current:

Current also throws an exception if the last call to MoveNext returned false, which indicates the end of the collection

However, this does not happen with iterator blocks. For example:

void Main()
{
    using (var enumerator = GetCounter().GetEnumerator())
    {
        for (int i = 0; i < 10; i++)
        {
            enumerator.MoveNext();
            Console.WriteLine (enumerator.Current);
        }
    }
}

static IEnumerable<int> GetCounter()
{
   for (int count = 0; count < 3; count++)
   {
       yield return count;
   }
}

Will simply print 8 times 2, no exceptions are thrown. Looking at the compiler transformation, Current is simply a field-backed property that always returns the field's value and nothing more. Perhaps it is some form of optimization? Still, this looks like a violation of the contract.

Was it helpful?

Solution

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))

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top