4

I don't understand this weird behavior. I have an overridden method that accepts int and a method that accepts an object type in derived class. If I pass an int, the method with object type is called.

using System;
public class Program
{
   static void Main(string[] args)
   {
      Derived d = new Derived();
      int i = 10;
      d.Func(i);
      Console.ReadKey();
  }
}
public class Base
{
  public virtual void Func(int x)
  {
      Console.WriteLine("Base.Func(int)");
  }
}
public class Derived : Base
{
  public override void Func(int x)
  {
      Console.WriteLine("Derived.Func(int)");
  }
  public void Func(object o)
  {
      Console.WriteLine("Derived.Func(object)");
  }
}

Why isn't the method that accepts int called?

https://www.ideone.com/OToXWS vs https://www.ideone.com/BIugxO

Ryan Leach
  • 4,262
  • 5
  • 34
  • 71
nan
  • 585
  • 2
  • 9
  • 22

0 Answers0