1

Why the below code would call the public void Func(object o) even if i call it with int like so int i = 10;

Is it because all types derived from System.Object?

using System;
public class Program
{
    static void Main(string[] args)
    {
        Derived d = new Derived();
        object 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)");
    }
}

please let me know why if it is correct.

Basil Kosovan
  • 888
  • 1
  • 7
  • 30
lokopi
  • 101
  • 1
  • 1
  • 10
  • 3
    Method overloads are chosen at compile-time, not runtime. C# only supports single-Dynamic Dispatch based on the type of `this`, it does not support Multiple-Dispatch ( https://en.wikipedia.org/wiki/Multiple_dispatch ) – Dai Oct 19 '19 at 11:06

0 Answers0