Nullable Set Operator

Trips

Well-known member
Joined
Aug 7, 2010
Messages
2,788
I am trying to understand the underline Nullable struct.
The line that confuses me is the following :
Nullable<int> value = null;
First the Nullable is a struct and structs cannot be set to null. Second the wrapper Nullable object isnt null. The inner "Value" of the object is considered null. This is why the HasValue returns false when the object is set
null and does not throw a NullReferenceException.
C# does not support set operator overloading. The two operator overloads are explicit T(Nullable<T> value), and implicit Nullable<T>(T value). The implicit conversion doesnt explain the set, because T is a struct and cannot
be set to null.
I know it might seem like a stupid question but how does this "Nullable<int> value = null;" work?
I guess it is special, just as the int? shortcut is.

View the full article
 
Back
Top