title: Why is the error handling for IEnumerator.Current different from IEnumerator<T>.Current?
body: <p>I would have thought that executing the following code for an empty collection that implements <code>IEnumerable&lt;T&gt;</code> would throw an exception:</p>
  
  <pre><code>var enumerator = collection.GetEnumerator();
  enumerator.MoveNext();
  var type = enumerator.Current.GetType(); // Surely should throw?
  </code></pre>
  
  <p>Because the collection is empty, then accessing <code>IEnumerator.Current</code> is invalid, and I would have expected an exception. However, no exception is thrown for <code>List&lt;T&gt;</code>.</p>
  
  <p>This is allowed by <a href="https://msdn.microsoft.com/en-us/library/58e146b7%28v=vs.110%29.aspx" rel="noreferrer">the documentation for <code>IEnumerator&lt;T&gt;.Current</code></a>, which states that <code>Current</code> is undefined under any of the following conditions:</p>
  
  <ul>
  <li>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.</li>
  <li>The last call to MoveNext returned false, which indicates the end of
  the collection.</li>
  <li>The enumerator is invalidated due to changes made in the collection, such as adding, modifying, or deleting elements.</li>
  </ul>
  
  <p>(I'm assuming that "fails to throw an exception" can be categorised as "undefined behaviour"...)</p>
  
  <p>However, if you do the same thing but use an <code>IEnumerable</code> instead, you DO get an exception. This behaviour is specified by <a href="https://msdn.microsoft.com/en-us/library/system.collections.ienumerator.current%28v=vs.110%29.aspx" rel="noreferrer">the documentation for <code>IEnumerator.Current</code></a>, which states:</p>
  
  <ul>
  <li>Current should throw an InvalidOperationException if the last call to MoveNext returned false, which indicates the end of the collection.</li>
  </ul>
  
  <p><strong>My question is: Why this difference? Is there a good technical reason that I'm unaware of?</strong> </p>
  
  <p>It means identical-seeming code can behave very differently depending on whether it's using <code>IEnumerable&lt;T&gt;</code> or <code>IEnumerable</code>, as the following program demonstrates (note how the code inside <code>showElementType1()</code> and <code>showElementType1()</code> is identical):</p>
  
  <pre><code>using System;
  using System.Collections;
  using System.Collections.Generic;
  
  namespace ConsoleApplication2
  {
      class Program
      {
          public static void Main()
          {
              var list = new List&lt;int&gt;();
  
              showElementType1(list); // Does not throw an exception.
              showElementType2(list); // Throws an exception.
          }
  
          private static void showElementType1(IEnumerable&lt;int&gt; collection)
          {
              var enumerator = collection.GetEnumerator();
              enumerator.MoveNext();
              var type = enumerator.Current.GetType(); // No exception thrown here.
              Console.WriteLine(type);
          }
  
          private static void showElementType2(IEnumerable collection)
          {
              var enumerator = collection.GetEnumerator();
              enumerator.MoveNext();
              var type = enumerator.Current.GetType(); // InvalidOperationException thrown here.
              Console.WriteLine(type);
          }
      }
  }
  </code></pre>