Extract from CLR via C# on Boxing / Unboxing value types …
On Boxing: If the nullable instance is not null, the CLR takes the value out of the nullable instance and boxes it. In other words a Nullable < Int32 > with a value of 5 is boxed into a boxed-Int32 with a value of 5.
On Unboxing: Unboxing is simply the act of obtaining a reference to the unboxed portion of a boxed object. The problem is that a boxed value type cannot be simply unboxed into a nullable version of that value type because the boxed value doesn’t have the boolean hasValue field in it. So, when unboxing a value type into a nullable version, the CLR must allocate a Nullable < T > object, initialize the hasValue field to true, and set the value field to the same value that is in the boxed value type. This impacts your application performance (memory allocation during unboxing).
Why did the CLR team go through so much trouble for Nullable types ? Why was it not simply boxed into a Nullable < Int32 > in the first place ?
I remember this behavior was kind of last minute change. In early betas of .NET 2.0,
Nullable<T>was a "normal" value type. Boxing anullvaluedint?turned it into a boxedint?with a boolean flag. I think the reason they decided to choose the current approach is consistency. Say:In the former approach (box
null-> boxedNullable<T>), you wouldn’t get "test is not null" but you’d get "object is not null" which is weird.Additionally, if they had boxed a nullable value to a
boxed-Nullable<T>:Beside that, I believe the current behavior makes perfect sense for scenarios like nullable database values (think SQL-CLR…)
Clarification:
The whole point of providing nullable types is to make it easy to deal with variables that have no meaningful value. They didn’t want to provide two distinct, unrelated types. An
int?should behaved more or less like a simpleint. That’s why C# provides lifted operators.This is not true. The CLR would have to allocates memory on stack to hold the variable whether or not it’s nullable. There’s not a performance issue to allocate space for an extra boolean variable.