शेखर
शेखर

Reputation: 17614

Type of null literal in C#

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

Answers (3)

BoltClock
BoltClock

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:

  1. 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.)

  2. 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 and T2, T1 is a better conversion target than T2 if at least one of the following holds:

    • An implicit conversion from T1 to T2 exists, and no implicit conversion from T2 to T1 exists

    • ...

Thus, the string overload is chosen as the best fit.

Upvotes: 33

Azodious
Azodious

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

  1. Find ShowText method.
  2. Oh, I've found 4, methods, which one to call?!!
  3. Lets see what argument is passed, hmm .. it's null.
  4. Find ShowText method which has reference type as argument. all overloads with primitive arguments are ignored.
  5. Oh No ... i can match it to eithe string OR object.
  6. Which is more specific. i.e which comes first in inheritance hierarchy from below.
  7. Hurray .. found it .. it's 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

SWeko
SWeko

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

Related Questions