tags:

views:

396

answers:

6

Does null inherit from Object as well in C#?

Where does the null itself defined?

+10  A: 

Does null inherit from Object as well in C#?

Unlike in some other systems, null is not defined in terms of a class or an instance. On the contrary, it signifies the absence of any instance, and it doesn't have a type. It is implicitly converted to whatever reference type fits the context. The widest conversion (i.e. when no other can be inferred) is to object. But still, null is not generally of type object.

Konrad Rudolph
Hmm, I'm not sure what you're saying by "...it [null] doesn't have a type... it is implicitly converted to whatever type fits the context... [it] is not generally of type object."
Chris
Chris, well, you could hypothetically think that `null` is actually a hard-coded constant of type `object` that just gets converted implicitly to other types depending on context. This is not the case.
Konrad Rudolph
Some people seem to take issue with my explanation. Please leave a comment saying what you object to!
Konrad Rudolph
+6  A: 

null is not an object - it is a language keyword that denotes the lack of an object reference.

Andrew Hare
That's they key--it's a *language keyword*, and it denotes the absence of an *object reference*.
Chris
Or a Nullable<T> value without a value (HasValue==false)
Marc Gravell
+3  A: 

Where does the null itself defined?

null is a keyword. null is not a type.

There are two kinds of variables, each with its own set of nullability rules.

Reference type variables

The variable is typed and can refer to instances of the same or different types.

//The reference type is System.Object and
//  the instance type is System.String
object s = "123";

With reference type variables, null indicates an absence of an instance.

//The reference type is System.Object and
//  there is no instance.
object x = null;

Using a reference type variable that has no instance will result in a null reference exception.

string s = null;
s = s + "a"; //BOOM.

Value type variables

Value type variables have a single type, and the values assigned must be of this type and no other.

int i = 3;

With certain value type variables (only those that are of the type Nullable<T>), null may be assigned. This indicates the absence of value.

int? i = null;

Using a value type that has no value does not generate null reference exceptions - there is no reference involved.

int? i = null
if (i < 3) //false
David B
A: 

null is not a type, it is a value. More specifically it is a value that is by definition the abscence of a value. In C# null is convertible to any reference type.

So null does not inherit from System.Object but it is a value convertible to any object which does not inherit from System.ValueType.

JaredPar
Don't forget the *other* use of the null keyword with Nullable<T> - to which it isn't exactly *convertible* - rather the compiler uses .HasValue etc as an alternative.
Marc Gravell
@Marc, I intentionally limited it to referenc types to avoid issues with Nullable ;)
JaredPar
+2  A: 

null is of the "null type", which does inherit from object.

See ECMA-334 11.2.7

Alun Harford
Per your suggestion I'm reading http://en.csharp-online.net/ECMA-334:_11.2_Reference_types While I would agree the "null type" is a Reference type, I'm not sure if it inherits from object.
Tom Lokhorst
11.2.2 says "Every type in C# directly or indirectly derives from the object class type."In the CLR itself, things are a little more complicated (System.TypedReference will report that it inherits from object, but cannot be cast to object) but in 'pure' C#, every type inherits from object.
Alun Harford
+1  A: 

I think null is not an object per se.