6

When I was writing C# code a few days ago I noticed that the compiler complained that I had to cast null to a specific object.

Does this mean that null is simply an uninstantiated version of the type? Or is it a singleton value of a Null class like Java (even though it has special privileges)?

EDIT: an example of the code giving the error would be:

public String duplicate(String toDuplicate)
{
    return toDuplicate + toDuplicate;
}

public String duplicate(int? toDuplicate)
{
    String asString = toDuplicate.toString();

    return asString + asString;
}

public static int Main(string[] args)
{
    //This needs to be cast:
    duplicate(null);
    //To:
    duplicate((string)null);
}

The reason I commented on null in Java was after reading this:

There is also a special null type, the type of the expression null, which has no name. Because the null type has no name, it is impossible to declare a variable of the null type or to cast to the null type. The null reference is the only possible value of an expression of null type. The null reference can always be cast to any reference type. In practice, the programmer can ignore the null type and just pretend that null is merely a special literal that can be of any reference type.

Found here: Is null an Object?

Community
  • 1
  • 1
Darkzaelus
  • 2,059
  • 1
  • 15
  • 31
  • 1
    Could you please show the line of code that was giving error? – FIre Panda Jun 03 '11 at 10:21
  • 1
    In Java `null` is a literal. It is not an instance of anything. There is no class which represents it. You cannot do `null.getClass()` – Peter Lawrey Jun 03 '11 at 10:21
  • The example code you are providing does not cause an error in Visual Studio 2010/NET 4.0. The compiler will be able to resolve all types. – Thorsten Dittmar Jun 03 '11 at 10:38
  • I've corrected it now Thorsten (I hope) – Darkzaelus Jun 03 '11 at 10:42
  • Nope, this doesn't cause an error either, as the compiler can tell that `null` can never be used with the `int`-version of the method. If you changed `int` to `int?` then an error would be caused, but this is understandable, because both `string` and `int?` are object references - so the compiler does not know which method to use as `null` does not have a type. – Thorsten Dittmar Jun 03 '11 at 10:49
  • Oops, i'm not quite on form today! Thanks Thorsten – Darkzaelus Jun 03 '11 at 10:50

6 Answers6

6

I get the error you refer when i have overloaded methods and the compiler can't resolve which method to call at compile time. Is that it?

Luis Filipe
  • 8,488
  • 7
  • 48
  • 76
5

According to the MSDN description:

The null keyword is a literal that represents a null reference, one that does not refer to any object. null is the default value of reference-type variables. Ordinary value types cannot be null. However, C# 2.0 introduced nullable value types.

Aykut Çevik
  • 2,060
  • 3
  • 20
  • 27
1

null is the "uninstanciated reference" for any type. It is not a value. null has no defined type.

jdehaan
  • 19,700
  • 6
  • 57
  • 97
1

No - the null is just a literal for the null reference.

The reason you need to "cast" it in this way (I put the word cast in quotes because you are not really casting an instance of anything), is purely to help the compiler resolve which overload of a method you are calling, or the method you are calling even exists.

In your example, you need to specify that you are calling the method "duplicate" that takes a single string argument. If you omit the cast, then the compiler only knows that the method you intended to call is called "duplicate" and has a single argument, but can't tell what type the argument is - did you mean to call duplicate(string) or did you mean to call duplicate(some other type)? Should it compile, or should it error telling you the method you are trying to call does not exist?

You will also get the same issue if you had a duplicate(byte[]) defined, because now your call is ambiguous without the explicit cast.

Rob Levine
  • 40,328
  • 13
  • 85
  • 111
0

No its not an object. null is the default value of reference-type variables. Ordinary value types cannot be null. but there is another set called nullable types.

Aravind
  • 4,125
  • 1
  • 28
  • 39
0

You are probably referring to the fact that the following leads to a compiler error:

int? nullableInt = (somecondition) ? value : null;

Indeed you need to add a cast here:

int? nullableInt = (somecondition) ? value : (int?)null;

Even though I'm not able to explain this in detail, I'd suspect the following:

int? is actually a short form for Nullable<int>, so it is basically an object instance. When assigning an int value to nullableInt, a property of Nullable<int> will be set internally. Directly assigning null would also be ok.

The conditional assignment however, would return two different types: int in case somecondition is true and object (null) otherwise.

Now the compiler doesn't know how to handle this, as the ternary operator needs to return values of the same type. So you need to specify the desired "type for the null value".

Sorry if this is not a very deep technical explanation - I'm sure there's somebody who can elaborate this better, but it might help understand this better.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • I also experienced this a while back and seems to be the same problem. Also your explanation was fine! perfectly understandable – Darkzaelus Jun 03 '11 at 10:36