Passing null value to overloading method where Object and String as param in C#

.net, c#

Solution

The C# compiler takes the most specific overload possible.

As `string` is an `object`, and it can have the value of `null`, the compiler deems `string` to be more specific.

Problem

I have two overloaded methods like below ``` public class TestClass { public void LoadTest(object param) { Console.WriteLine("Loading object..."); } public void LoadTest(string param) { Console.WriteLine("Loading string..."); } } ``` After calling this method like below it will show the output as Loading string... Please explain how .net handle this scenario? ``` class Program { static void Main(string[] args) { var obj=new TestClass(); obj.LoadTest(null); // obj.LoadType(null); Console.ReadLine(); } } ```

Original source

Related problems