在VB.NET中如下代码:
Dim myBool As Boolean = False
Dim myDate As Date? = If(myBool, Date.Today, Nothing)
Console.WriteLine(myDate)
生产:
1/1/0001 12:00:00 AM
但是,如果我将其写为:
Dim myBool As Boolean = False
Dim myDate As Date?
If myBool Then
myDate = Date.Today
Else
myDate = Nothing
End If
Console.WriteLine(myDate)
然后它会如预期的那样打印 null/nothing。
这是怎么回事,我怎样才能用一个单行代码将日期或 null 分配给 VB.NET 中的可空 DateTime,而不使其实际上默认为01/01/0001 12:00:00 AM
?