假设有以下代码:
Dim widthStr As String = Nothing
这个有效 -
width
被赋值为 Nothing
:Dim width As Nullable(Of Double)
If widthStr Is Nothing Then
width = Nothing
Else
width = CDbl(widthStr)
End If
但是这样做不会起作用 - width
变成了 0.0
(尽管代码在逻辑上看起来是相同的):
Dim width As Nullable(Of Double) = If(widthStr Is Nothing, Nothing, CDbl(widthStr))
为什么会这样?有什么我可以做的来使它工作吗?
double? width = widthStr != null ? Double.Parse(widthStr) : (double?)null;
- Federico Berasateguinull
和VB.Net中的Nothing
是不等价的。Nothing
大约相当于default(T)
。 - Damien_The_Unbeliever