What is the type of null literal?

According to the ECMA C# language specification:

9.4.4.6 The null literal:

The type of a null-literal is the null type (§11.2.7).

11.2.7 The null type:

The null literal (§9.4.4.6) evaluates to the null value, which is used
to denote a reference not pointing at any object or array, or the
absence of a value. The null type has a single value, which is the
null value. Hence an expression whose type is the null type can
evaluate only to the null value. There is no way to explicitly write
the null type and, therefore, no way to use it in a declared type.
Moreover, the null type can never be the type inferred for a type
parameter (§25.6.4)

So to answer your question, null is it’s own type – the null type.

Although it’s odd how it’s not mentioned in the C# 4.0 language specification or the C# 3.0 language specification but is mentioned in the overview of C# 3.0, the ECMA C# language specification and the C# 2.0 language specification.

Leave a Comment