title: Different behaviour of method overloading in C#
body: <p>I was going through C# Brainteasers (<a href="http://www.yoda.arachsys.com/csharp/teasers.html" rel="noreferrer">http://www.yoda.arachsys.com/csharp/teasers.html</a>) and came across one question: what should be the output of this code?</p>
  
  <pre><code>class Base
  {
      public virtual void Foo(int x)
      {
          Console.WriteLine ("Base.Foo(int)");
      }
  }
  
  class Derived : Base
  {
      public override void Foo(int x)
      {
          Console.WriteLine ("Derived.Foo(int)");
      }
  
      public void Foo(object o)
      {
          Console.WriteLine ("Derived.Foo(object)");
      }
  }
  
  class Test
  {
      static void Main()
      {
          Derived d = new Derived();
          int i = 10;
          d.Foo(i);  // it prints ("Derived.Foo(object)"
      }
  } 
  </code></pre>
  
  <p>But if I change the code to </p>
  
  <pre><code>class Derived 
  {
      public void Foo(int x)
      {
          Console.WriteLine("Derived.Foo(int)");
      }
  
      public void Foo(object o)
      {
          Console.WriteLine("Derived.Foo(object)");
      }
  }
  
  class Program
  {
      static void Main(string[] args)
      {
          Derived d = new Derived();
          int i = 10;
          d.Foo(i); // prints  Derived.Foo(int)");
  
          Console.ReadKey();
      }
  }
  </code></pre>
  
  <p>I want to why the output is getting changed when we are inheriting vs not inheriting; why is method overloading behaving differently in these two cases?     </p>