相关疑难解决方法(0)

为什么在VB.NET和C#中检查null值是否存在差异?

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
Run Code Online (Sandbox Code Playgroud)

但在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");
}
Run Code Online (Sandbox Code Playgroud)

为什么会有区别?

c# vb.net null .net-4.0

108
推荐指数
4
解决办法
5580
查看次数

标签 统计

.net-4.0 ×1

c# ×1

null ×1

vb.net ×1