Possible Duplicate:
C#: Passing null to overloaded method - which method is called?
Here is a test case
object a = null;
var b = Convert.ToString (null);
var c = Convert.ToString (a);
string d = Convert.ToString (null); // CLR chooses Convert.ToString(string value)
string e = Convert.ToString (a); // CLR chooses Convert.ToString(object value)
The question is why CLR decides that null is interpreted as string in first case? It appears that this question was already answered here
Here is another similar case. None of these ifs are triggered
object x = null;
if (x is object)
{
Console.Write ("x is object");
}
if (x is string)
{
Console.Write ("x is string");
}
if (null is object)
{
Console.Write ("null is object");
}
if (null is string)
{
Console.Write ("null is string");
}
Method Overloading in Java is one of the most useful features of an Object-Oriented Language. It allows a class to have multiple methods with the same name. The only difference that these methods have is the different list of parameters that are passed through these methods.
Method overloading is an example of static binding where binding of method call to its definition happens at Compile time.
The compiler does not consider the return type while differentiating the overloaded method. But you cannot declare two methods with the same signature and different return type. It will throw a compile-time error. If both methods have the same parameter types, but different return type, then it is not possible.
If we try to define more than one method with the same name and the same number of arguments then the compiler will throw an error. The advantage of method overloading is that it increases code readability and maintainability.
The answer is because it must choose a reference type (null doesn't work for value types), and every string
is an object
, but not every object
is a string
. See Jon Skeet's answer to this question for more information.
In response to your second example, if a variable that is null is passed to is
, it will always evaluate to false, no matter what.
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