Possible Duplicate:
C#: Passing null to overloaded method - which method is called?
Consider these 2 methods:
void Method(object obj) { Console.WriteLine("object"); }
void Method(int[] array) { Console.WriteLine("int[]"); }
When I try calling:
Method(null);
in Visual Studio 2008 SP1 I get int[]
.
Why is this?
It is a product of overload resolution. Your argument, null
, is convertible to both object
and int[]
. The compiler therefore picks the most specific version, because int[]
is more specific than object
.
Because int[] is more specific than object, the method with the object-parameter will be ignored. If you would call Method("Some string"), the method with the object-parameter will be called.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With