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();
}
}
You cannot pass the null value as a parameter to a Java scalar type method; Java scalar types are always non-nullable. However, Java object types can accept null values. The values of both variable <@I> and variable <@A> are null, since values have not been assigned to them.
It is a value of the reference variable. The access to a null reference generates a NullPointerException. It is not allowed to pass null as a value to call the methods that contain any primitive data type.
No, 'null' is a value. In java you are passing arguments to function as reference (except basic types), which value (of this reference) 'points' to object. In this case value of reference points to nothing.
When we pass a null value to the method1 the compiler gets confused which method it has to select, as both are accepting the null. This compile time error wouldn't happen unless we intentionally pass null value.
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.
null
is a valid string
.
It will try to match the most specific type and string
is more specific than object
Depending on your use you should probably remove the parameter totally in the object
overload.
This is just the way C# compiler prioritizes to determine which method is better to call. There is one rule:
If method A has more specific parameter types than method B, then method A is better than method B in overload case.
In your case, apparently string
is more specified than object
, that is why LoadTest(string param)
is called.
You can refer 7.5.3.2 Better function member in C# language specification to get more understanding.
because compiler takes the closest possible specific method that can be accessed (null to string is closer than null to object) that is available. And in this case as both as overload with string is closer, that is why it is called.
This is what MSDN has to say
Once the candidate function members and the argument list have been identified, the selection of the best function member is the same in all cases:
Given the set of applicable candidate function members, the best function member in that set is located.
If the set contains only one function member, then that function member is the best function member.
Otherwise, the best function member is the one function member that is better than all other function members with respect to the given argument list, provided that each function member is compared to all other function members using the rules in Section 7.4.2.2.
If there is not exactly one function member that is better than all other function members, then the function member invocation is ambiguous and a compile-time error occurs.
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