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.
The null in Java is a literal of the null type. It cannot be cast to a primitive type such as int. float etc. but can be cast to a reference type.
The C and C++ languages have a null character (NUL), a null pointer (NULL), and a null statement (just a semicolon (;)). The C NUL is a single character that compares equal to 0. The C NULL is a special reserved pointer value that does not point to any valid data object.
Null is a reserved keyword in the Java programming language. It's technically an object literal similar to True or False. Null is case sensitive, like any other keyword in Java. When programming in Java, it's important to write null in lowercase.
In C#, you can assign the null value to any reference variable. The null value simply means that the variable does not refer to an object in memory. You can use it like this: Circle c = new Circle(42); Circle copy = null; // Initialized ... if (copy == null) { copy = c; // copy and c refer to the same object ... }
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.
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.
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