With VB's Option Strict On
, why does a Nullable(Of T)
not require an explicit cast to an interface of T when it does require one to T
?
I.e.
Dim x As Integer? = 5
Dim y As Integer
Dim z As IComparable
y = x ' Fails to compile with error
' "Option Strict On disallows implicit conversions from 'Integer?' to 'Integer'."
z = x ' Succeeds
EDIT: As (sort of) shown by @SSS, part of the answer is that Nullable
values are, well, nullable, and can be Nothing
, which is fine for a reference like an interface. So this conversion will always succeed, unlike the conversion to T
case (which fails when the Nullable
has no value), and so it can be seen as an implicit conversion.
My question now becomes "how?". How is the conversion from a Nullable(Of T)
(which has no interfaces of its own) to an interface of T
theoretically negotiated?
I know the implementation is box Nullable<T>
, which effectively strips the Nullable
wrapper, but I'm confirming the concept here...
(So I'll review the documentation and see if they explain this.)