title: Why does foreach(var i in array2D) work on multidimensional arrays?
body: <p>Given the following C# code:</p>
  
  <pre><code>   int[,] array2D = new int[10, 10];
     int sum = 0;
  
     foreach (var i in array2D)
     {
          sum += i;
     }
  </code></pre>
  
  <p>The question is: what is causing the type of <code>i</code> to be correctly inferred as an <code>int</code>? </p>
  
  <p>This is not at all obviuous, as array2D is a rectangular array. It does not implement <code>IEnumerable&lt;int&gt;</code>. It also implements a <code>GetEnumerator()</code> method, which returns a <code>System.Collections.IEnumerator</code>. I would therefore expect that <code>i</code> would be of type <code>object</code>. </p>
  
  <p>My code is using .net 4.03. </p>
  
  <p>Related SO question: <a href="https://stackoverflow.com/questions/275073/why-do-c-sharp-multidimensional-arrays-not-implement-ienumerablet">Why do C# Multidimensional arrays not implement IEnumerable?</a>. </p>