Nullable is not a ValueType

Viewed 325

If you look at the documentation for the .NET Nullable, you'll see:

   struct Nullable<T>

Note that it's a struct, not a class.

It seems a struct Nullable<T> is not a ValueType, which is very unexpected. The following code prints False:

   Type nullableType = typeof(Nullable<int>);
   Console.WriteLine(nullableType is ValueType);

If you look at the generated IL you'll see that that compiler determined nullableType to be a ValueType at compile time. But how can it be a ValueType if it's a struct? All structs are ValueTypes, right? Obviously, it has something to do with the generic.

What am I missing here? Is there something in the language spec about this?

Thanks.

3 Answers
Related