Reputation: 17614
I have a query about type of null.
I have a small program can anyone tell me about this.
public class TestApplication
{
public void ShowText(object ob)
{
Console.Write("Inside object");
}
public void ShowText(string str)
{
Console.Write("Inside string");
}
public void ShowText(int i)
{
Console.Write("Inside int.");
}
public void ShowText(char c)
{
Console.Write("Inside Character");
}
static void Main(string[] args)
{
new TestApplication().ShowText(null);
Console.Read();
}
}
Why it call the string function.
Is it means the type of null is string.
It might look a foolish conclusion but I am not able to find the region why it is calling the function of string.
Upvotes: 20
Views: 8690
Reputation: 724142
Your question about the type of the null literal is answered here: What is the type of null literal?
But that doesn't really matter when talking about overload resolution. The actual null value itself will automatically be converted to whatever type it ends up as.
As for why the string
overload is called:
You can't pass null as int
and char
parameters as they're value types, so those two overloads are out. (They could have been candidates had you made them nullable int?
and char?
types, but I won't go into that.)
Between the two other overloads taking reference types, string
is a more specific type than object
. That is, it derives from (and is therefore implicitly convertible to) object
. Further, from the following section in the C# language spec (emphasis mine):
7.5.3.5 Better conversion target
Given two different types
T1
andT2
,T1
is a better conversion target thanT2
if at least one of the following holds:
An implicit conversion from
T1
toT2
exists, and no implicit conversion fromT2
toT1
exists...
Thus, the string
overload is chosen as the best fit.
Upvotes: 33
Reputation: 13882
null
can fit to any reference-type. and hence is a very good example for polymorphism.
lets say you have a Vehicle
class. you create three more class Two-Wheeler
, Three-Wheeler
, Four-Wheeler
.
if latter 3 class extends
Vehicle
class; it is called Vehicle
is specified into those three categories.
So, given a value, say car, more specific fit to this value is Four-Wheeler
.
Similar is the case with null
. it can fit to either Object
OR String
; but more specific match is String
.
Lets see how compiler thinks (specific to your code only) when it sees a call to ShowText(null)
method
ShowText
method.null
.ShowText
method which has reference type as argument. all overloads with primitive arguments are ignored.string
OR object
.string
.. lets call it.And, for an exercise if you want to undestand that what will happen if compiler finds at step 6 mor than one matches ... define a ShowText(string[] vals) and see yourself.
Upvotes: 6
Reputation: 30902
This is an example of the overload resolution in C#, it does not prove that the type of the null
is string.
null
is a special value that is included in the domain of any reference type, it basically determines that there is not a valid reference. As such, it can be a string
, an int?
, an object
etc, etc.
You can even cast null
to any reference type, to get a "correctly" typed null
, e.g. you must cast null if it's used as a value in a ?
operator
int? value = (i > 0) ? i : null; // does not compile
int? value = (i > 0) ? i : (int?)null; //works
Upvotes: 3