1

I came across the below lines of code recently.

public class Program
    {
        public Program(Object o)
        {
            Console.WriteLine("Object");
        }
        public Program(double[] dArray)
        {
            Console.WriteLine("double array");
        }
        public static void Main(string[] args)
        {
            new Program(null);
        }
    }

I thought the constructor with object as input parameter will be invoked whereas the constructor with input parameter as double array was invoked. I was confused here. Can anyone help me understand the reason behind this?

Harry
  • 75
  • 8
  • 1
    It's not about constructors only, this belongs to method overloading in general – Pavel Anikhouski Feb 29 '20 at 19:46
  • 1
    Have a look at this thread [How does the method overload resolution system decide which method to call when a null value is passed?](https://stackoverflow.com/questions/5173339/how-does-the-method-overload-resolution-system-decide-which-method-to-call-when) There is a nice explanation from Eric Lippert about overloading resolution – Pavel Anikhouski Feb 29 '20 at 19:47

0 Answers0