Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type of null literal in C#

Tags:

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.

like image 793
शेखर Avatar asked Jan 17 '12 09:01

शेखर


People also ask

What type of literal is null?

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.

What is the type of null in C?

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.

What type of literal is null in Java?

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.

What is a null value in C#?

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 ... }


2 Answers

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.

like image 81
BoltClock Avatar answered Sep 30 '22 05:09

BoltClock


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.

like image 33
Azodious Avatar answered Sep 30 '22 04:09

Azodious