This all comes down to type analysis of expressions.
Nothing
is a magical beast in VB.Net. It's approximately the same as default(T)
in C#.
As such, when trying to determine the best type for the following:
If(widthStr Is Nothing, Nothing, CDbl(widthStr))
The third argument is of type Double
. The second argument is convertible to Double
(because Nothing
can return the default value of value types). As such, the type of the return value of If
is determined to be Double
.
Only after that piece of type analysis has concluded is any attention paid to the type of the variable to which this expression is being assigned. And Double
is assignable to Double?
without any warnings.
There's no clean way to make your If()
expression work how you expected. Because there's no equivalent to null
in VB.Net. You'd need (at the least) to insert a DirectCast
(or equivalent) on one side or another of the potential results of the If
to force the type analysis to see Double?
rather than Double
.