title: How can C# allow virtual generic methods where C++ can't allow virtual template methods?
body: <p>C++ does not support virtual template methods. The reason is that this would alter the <code>vtable</code> whenever a new instantiation of such a method is made (it has to be added to the <code>vtable</code>).</p>
  
  <p>Java in contrast does allow virtual generic methods. Here, it is also clear how this can be implemented: Java generics are erased at runtime, so a generic method is a usual method at runtime, so no alterations of the <code>vtable</code> necessary.</p>
  
  <p>But now to C#. C# does have reified generics. With reified generics and especially when using value types as type parameters, there have to be different versions of a generic method. But then we have the same problem as C++ has: We would need to alter the vtable whenever a new instantiation of a generic method was made.</p>
  
  <p>I am not too deep into the inner workings of C#, so my intuition could simply be totally wrong. So can someone with more in-depth knowledge about C#/.NET tell me how they are able to implement generic virtual methods in C#?</p>
  
  <p>Here's code to show what I mean:</p>
  
  <pre><code>[MethodImpl(MethodImplOptions.NoInlining)]
  static void Test_GenericVCall()
  {
      var b = GetA();
      b.M&lt;string&gt;();
      b.M&lt;int&gt;();
  }
  
  [MethodImpl(MethodImplOptions.NoInlining)]
  static A GetA()
  {
      return new B();
  }
  
  class A
  {
      public virtual void M&lt;T&gt;()
      {
      }
  }
  
  class B : A
  {
      public override void M&lt;T&gt;()
      {
          base.M&lt;T&gt;();
          Console.WriteLine(typeof(T).Name);
      }
  }
  </code></pre>
  
  <p>How does the CLR dispatch to the correct JITed code when calling <code>M</code> in the function <code>Test_GenericVCall</code>?</p>