考慮下面的代碼:帶空格的可空類型如果不能一起工作?
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))
爲什麼?有什麼我可以做的嗎?
你在C#中嘗試相同嗎? '翻番? width = widthStr!= null? Double.Parse(widthStr):(double?)null;' –
請檢查http://stackoverflow.com/questions/14633824/nullable-type-with-inline-if-cannot-work-together http:// stackoverflow。 com/questions/2647558 /保存 - 雙變量中的空值 – spajce
@HighCore - C#中的'null'和VB.Net中的'Nothing'*不等同。 '什麼都不是',大約'default(T)'。 –