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 ?
Boxing and unboxing An instance of a nullable value type T? is boxed as follows: If HasValue returns false , the null reference is produced. If HasValue returns true , the corresponding value of the underlying value type T is boxed, not the instance of Nullable<T>.
Boxing and unboxing enables a unified view of the type system wherein a value of any type can ultimately be treated as an object. With Boxing and unboxing one can link between value-types and reference-types by allowing any value of a value-type to be converted to and from type object.
Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type. When the common language runtime (CLR) boxes a value type, it wraps the value inside a System. Object instance and stores it on the managed heap. Unboxing extracts the value type from the object.
Boxing and unboxing are important concepts in C#. The C# Type System contains three data types: Value Types (int, char, etc), Reference Types (object) and Pointer Types. Basically, Boxing converts a Value Type variable into a Reference Type variable, and Unboxing achieves the vice-versa.
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 a null
valued int?
turned it into a boxed int?
with a boolean flag. I think the reason they decided to choose the current approach is consistency. Say:
int? test = null; object obj = test; if (test != null) Console.WriteLine("test is not null"); if (obj != null) Console.WriteLine("obj is not null");
In the former approach (box null
-> boxed Nullable<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>
:
int? val = 42; object obj = val; if (obj != null) { // Our object is not null, so intuitively it's an `int` value: int x = (int)obj; // ...but this would have failed. }
Beside that, I believe the current behavior makes perfect sense for scenarios like nullable database values (think SQL-CLR...)
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 simple int
. That's why C# provides lifted operators.
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).
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.
I think it makes sense to box a null value to a null reference. Having a boxed value saying "I know I would be an Int32
if I had a value, but I don't" seems unintuitive to me. Better to go from the value type version of "not a value" (a value with HasValue
as false) to the reference type version of "not a value" (a null reference).
I believe this change was made on the feedback of the community, btw.
This also allows an interesting use of as
even for value types:
object mightBeADouble = GetMyValue(); double? unboxed = mightBeADouble as double?; if (unboxed != null) { ... }
This is more consistent with the way "uncertain conversions" are handled with reference types, than the previous:
object mightBeADouble = GetMyValue(); if (mightBeADouble is double) { double unboxed = (double) mightBeADouble; ... }
(It may also perform better, as there's only a single execution time type check.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With