title: Overloading methods in inherited classes
body: <p>I have started to understand that I do not understand what is going on. There is the following behavior in C#:</p>
  
  <pre><code>public class Base
  {
      public void Method(D a)
      {
          Console.WriteLine("public void Method(D a)");
      }
  }
  
  public class Derived: Base
  {
      public void Method(B a)
      {
          Console.WriteLine("public void Method(B a)");
      }
  }
  
  public class B { }
  
  public class D: B { }
  
  class Program
  {
      static void Main(string[] args)
      {
          Derived derived = new Derived();
          D d = new D();
  
          derived.Method(d);
      }
  }
  </code></pre>
  
  <p>It will print </p>
  
  <p><code>public void Method(B a)</code></p>
  
  <p>instead of </p>
  
  <p><code>public void Method(D a)</code> </p>
  
  <p>It's surprising. I suppose that the reason of this behavior is implementation of methods table. CLR does not search methods in the base class if it finds the corresponding method in the current type. I think they were trying to improve performance.</p>
  
  <p>But I was completely disappointed with the following code:</p>
  
  <pre><code>public class Base
  {
      public virtual void Method(D a)
      {
          Console.WriteLine("public void Method(D a)");
      }
  }
  
  public class Derived: Base
  {
      public override void Method(D a)
      {
          Console.WriteLine("public override void Method(D a)");
      }
  
      public void Method(B a)
      {
          Console.WriteLine("public void Method(B a)");
      }
  
  }
  
  public class B { }
  
  public class D: B { }
  
  class Program
  {
      static void Main(string[] args)
      {
          Derived derived = new Derived();
          D d = new D();
  
          derived.Method(d);
      }
  }
  </code></pre>
  
  <p>and it will print </p>
  
  <p><code>public void Method(B a)</code></p>
  
  <p>instead of </p>
  
  <p><code>public override void Method(D a)</code></p>
  
  <p>It's awful and very unpredictable.</p>
  
  <p>Can anyone explain it? </p>
  
  <p>I suppose that the method table has methods that have been implemented in the current type only (excluding overriding methods) and CLR stops looking for the corresponding method as soon as any method that can be called is found. Am I right?</p>