Are nullable types reference types?

Vaibhav Jain picture Vaibhav Jain · Jun 30, 2010 · Viewed 26.1k times · Source

When I declare an int as nullable

int? i=null;

Does i here become a reference type?

Answer

kemiller2002 picture kemiller2002 · Jun 30, 2010

No, a nullable is a struct. What is happening is that the nullable struct has two values:

  1. The value of the data type (int for int?, DateTime for DateTime?, etc.).
  2. A boolean value which tells if the data type value has been set. (HasValue is the property.)

When you set the value of the data type, the struct changes HasValue to true.

Nullable types (C# Programming Guide)