title: Why does the C# compiler crash on this code?
body: <p>Why does the code below crash the .NET compiler? It was tested on csc.exe version 4.0.</p>
  
  <p>See e.g. here for online demo on different version - it crashes in the same manner while it says dynamic is not supported <a href="https://dotnetfiddle.net/FMn59S" rel="nofollow">https://dotnetfiddle.net/FMn59S</a>:</p>
  
  <blockquote>
    <p>Compilation error (line 0, col 0): Internal Compiler Error (0xc0000005 at address xy): likely culprit is 'TRANSFORM'.</p>
  </blockquote>
  
  <p>The extension method works fine on <code>List&lt;dynamic&gt;</code> though.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  
  static class F  {
      public static void M&lt;T&gt;(this IEnumerable&lt;T&gt; enumeration, Action&lt;T&gt; action){}
  
      static void U(C.K d) {
          d.M(kvp =&gt; Console.WriteLine(kvp));
      }
  }
  
  class C  {
      public class K : Dictionary&lt;string, dynamic&gt;{}
  }
  </code></pre>
  
  <p>Update: this doesn't crash the compiler</p>
  
  <pre><code>static void U(Dictionary&lt;string, dynamic&gt; d)
  {
      d.M(kvp =&gt; Console.WriteLine(kvp));
  }
  </code></pre>
  
  <p>Update 2: the same bug was reported in <a href="http://connect.microsoft.com/VisualStudio/feedback/details/892372/compiler-error-with-dynamic-dictinoaries" rel="nofollow">http://connect.microsoft.com/VisualStudio/feedback/details/892372/compiler-error-with-dynamic-dictinoaries</a>. The bug was reported for FirstOrDefault, but it seems the compiler crashes on any extension method applied to class derived from Dictionary&lt;T1,T2>, where at least one of the parameter types is <code>dynamic</code>. See an even more general description of the problem below by Erik Funkenbusch.</p>
  
  <p>Update 3: another non-standard behaviour. When I try to call extension method as a static method, that is, <code>F.M(d, kvp =&gt; Console.WriteLine(kvp));</code>, the compiler doesn't crash, but it cannot find the overload:
  <code>Argument 1: cannot convert from 'C.K' to 'System.Collections.Generic.IEnumerable&lt;System.Collections.Generic.KeyValuePair&lt;string,dynamic&gt;&gt;'</code></p>
  
  <p>Update 4 - SOLUTION (kind of): Hans sketched 2nd workaround, which is semantically equivalent to original code, but works only for extension method call and not for standard call. Since the bug is likely caused by the fact that the compiler fails to cast class derived from generic class with multiple parameters (with one being dynamic) to its supertype, the solution is to provide an explicit cast. See <a href="https://dotnetfiddle.net/oNvlcL" rel="nofollow">https://dotnetfiddle.net/oNvlcL</a>:</p>
  
  <pre><code>((Dictionary&lt;string, dynamic&gt;)d).M(kvp =&gt; Console.WriteLine(kvp));
  M((Dictionary&lt;string, dynamic&gt;)d, kvp =&gt; Console.WriteLine(kvp));
  </code></pre>