Nullable types, whats going on when you do int?

Published on Mar 02, 2009

As you know some types are not nullable like: int, decimal, double or DateTime. So if for some reason we need a null int we need to use the nullable types.

To define a nullable type we use the question mark notation like this: int?, decimal?, double? or DateTime?.

But what happened on the background? How are those types interpreted by the runtime?

Let’s take the following code.

Compile it and use Reflector to take a look at the result:

Opening the resulting assembly with reflector shows no changes if we see the code as C# but take a look at the IL code:

Look at the type of the fields they are all of System.Nullable<T>. This type is defined in mscorlib as follow.

Interesting, isn’t it?