VB.NET, nullables and the ternary operator:
Dim i As Integer? = If(True, Nothing, 5)
This took me some time to debug, since I expected i
to contain Nothing
.
What does i really contain? 0
.
This is surprising but actually "correct" behavior: Nothing
in VB.NET is not exactly the same as null
in CLR: Nothing
can either mean null
or default(T)
for a value type T
, depending on the context. In the above case, If
infers Integer
as the common type of Nothing
and 5
, so, in this case, Nothing
means 0
.