【发布时间】:2013-03-20 12:50:39
【问题描述】:
在VB.NET 中会发生这种情况:
Dim x As System.Nullable(Of Decimal) = Nothing
Dim y As System.Nullable(Of Decimal) = Nothing
y = 5
If x <> y Then
Console.WriteLine("true")
Else
Console.WriteLine("false") '' <-- I got this. Why?
End If
但在 C# 中会发生这种情况:
decimal? x = default(decimal?);
decimal? y = default(decimal?);
y = 5;
if (x != y)
{
Debug.WriteLine("true"); // <-- I got this -- I'm with you, C# :)
}
else
{
Debug.WriteLine("false");
}
为什么会有差异?
【问题讨论】:
-
太可怕了。
-
我相信
default(decimal?)返回 0,而不是null。 -
@RyanFrame NO.由于它是可空类型,它返回
null -
哦,是的......对......在VB中
If条件不需要评估为布尔值...... uuuugh编辑:所以Nothing <> Anything = Nothing结果在If中采取否定/其他路线。 -
@JMK:Null、Nothing 和 Empty 实际上都有细微的不同。 如果它们都一样,那么你就不需要三个了。