title: C# 4.0 'dynamic' and foreach statement
body: <p>Not long time before I've discovered, that new <code>dynamic</code> keyword doesn't work well with the C#'s <code>foreach</code> statement:</p>
  
  <pre><code>using System;
  
  sealed class Foo {
      public struct FooEnumerator {
          int value;
          public bool MoveNext() { return true; }
          public int Current { get { return value++; } }
      }
  
      public FooEnumerator GetEnumerator() {
          return new FooEnumerator();
      }
  
      static void Main() {
          foreach (int x in new Foo()) {
              Console.WriteLine(x);
              if (x &gt;= 100) break;
          }
  
          foreach (int x in (dynamic)new Foo()) { // :)
              Console.WriteLine(x);
              if (x &gt;= 100) break;
          }
      }
  }
  </code></pre>
  
  <p>I've expected that iterating over the <code>dynamic</code> variable should work completely as if the type of collection variable is known at compile time. I've discovered that the second loop actually is looked like this when is compiled:</p>
  
  <pre><code>foreach (object x in (IEnumerable) /* dynamic cast */ (object) new Foo()) {
      ...
  }
  </code></pre>
  
  <p>and every access to the x variable results with the dynamic lookup/cast so C# ignores that I've specify the correct <code>x</code>'s type in the foreach statement - that was a bit surprising for me... And also, C# compiler completely ignores that collection from dynamically typed variable may implements <code>IEnumerable&lt;T&gt;</code> interface!</p>
  
  <p>The full <code>foreach</code> statement behavior is described in the C# 4.0 specification <strong>8.8.4 The foreach statement</strong> article.</p>
  
  <p>But... It's perfectly possible to implement the same behavior at runtime! It's possible to add an extra <code>CSharpBinderFlags.ForEachCast</code> flag, correct the emmited code to looks like:</p>
  
  <pre><code>foreach (int x in (IEnumerable&lt;int&gt;) /* dynamic cast with the CSharpBinderFlags.ForEachCast flag */ (object) new Foo()) {
      ...
  }
  </code></pre>
  
  <p>And add some extra logic to <code>CSharpConvertBinder</code>:</p>
  
  <ul>
  <li>Wrap <code>IEnumerable</code> collections and <code>IEnumerator</code>'s to <code>IEnumerable&lt;T&gt;</code>/<code>IEnumerator&lt;T&gt;</code>.</li>
  <li>Wrap collections doesn't implementing <code>Ienumerable&lt;T&gt;</code>/<code>IEnumerator&lt;T&gt;</code> to implement this interfaces.</li>
  </ul>
  
  <p>So today <code>foreach</code> statement iterates over <code>dynamic</code> completely different from iterating over statically known collection variable and completely ignores the type information, specified by user. All that results with the different iteration behavior (<code>IEnumarble&lt;T&gt;</code>-implementing collections is being iterated as only <code>IEnumerable</code>-implementing) and more than <code>150x</code> slowdown when iterating over <code>dynamic</code>. Simple fix will results a much better performance:</p>
  
  <pre><code>foreach (int x in (IEnumerable&lt;int&gt;) dynamicVariable) {
  </code></pre>
  
  <p>But why I should write code like this?</p>
  
  <p>It's very nicely to see that sometimes C# 4.0 <code>dynamic</code> works completely the same if the type will be known at compile-time, but it's very sadly to see that <code>dynamic</code> works completely different where IT CAN works the same as statically typed code.</p>
  
  <p>So my question is: why <code>foreach</code> over <code>dynamic</code> works different from <code>foreach</code> over anything else?</p>