This is what the Nullable(Of T)
type is for. Prior to the introduction of generics in .NET 2.0 (which made this type possible), any value type that wished to expose a semantic "empty" value had to do so on its own. This was usually presented by the structure using a "reserved value" to represent "empty", as you see with Point
, where any Point
at (0, 0)
is considered to be "empty". There is no way to distinguish between a Point
that is empty and a Point
that represents the origin. Other ways were contrived to represent emtpy values by using a boolean flag, but the way that these types were used was unintuitive (in that the consuming code still had a value in the variable).
Rather than force every value type that wished to support the concept of an empty or null value, the Nullable(Of T)
type was introduced. This essentially took the latter (i.e. boolean flag) concept and made it available for each and every value type, both system and user-defined. It also allows comparisons between a Nullable(Of T)
and Nothing
(null
in C#) to check for the presence of a value.
One thing that should be made clear, however, is that (as a value type itself), the Nullable(Of T)
structure is never actually a null reference; the ability to compare it against Nothing
is purely a language feature that's present both in VB.NET and C#. Under the covers, all such comparisons are actually checking the HasValue
property on the structure. Additionally, accessing the Value
property of the structure throws an exception if HasValue
is false.