Why does Nullable<T> HasValue property not throw NullReferenceException on Nulls?

Consider the following code:

DateTime? ndate = null;
Console.WriteLine(ndate.HasValue);

I would have expected a NullReferenceException, but HasValue will indeed return false. However, since ndate is null, how does the property invocation succeed, since there is no object to invoke the property HasValue on?


Technically, "ndate" is not null - it is a value type, with it's value specified as being null.

When you write DateTime?, this is just shorthand for Nullable<DateTime>, which is a struct. There is no way for this to technically be null, since it's not a reference type.


The Nullable<T> is a struct, basically it can't hold a null value.

Your assignment, is actually compiled into something that looks like this:

Nullable<DateTime> ndate = new Nullable<DateTime>();

Another example, the expression:

int? a = null;

Will generate the following IL:

.locals init (valuetype [mscorlib]System.Nullable`1<int32> V_0)
IL_0000:  ldloca.s   V_0
IL_0002:  initobj    valuetype [mscorlib]System.Nullable`1<int32>

A call to the initobj operation, which initializes each field of the a value type at a specified address to a null reference or a 0 of the appropriate primitive type.

In conclusion, what's happening here is the default struct initialization.