我试过这两个 block ,但它返回相同的值。就不多说了,直接上代码:
Dim f As Nullable(Of Integer)
If f = 1 Then
Console.WriteLine("Equal")
Else
Console.WriteLine("Not Equal")
End If
提示我“不等于”
我只是添加了一个 NOT,我希望得到 NOT 答案,但我得到的结果和上面一样!!!
Dim f As Nullable(Of Integer)
If Not f = 1 Then
Console.WriteLine("Equal")
Else
Console.WriteLine("Not Equal")
End If
它在 C# 中正确工作...
最佳答案
TL;DR:它根据为 VB 指定的行为正常工作,这与为 C# 指定的行为不同。
比较“f = 1”在 VB 中产生一个 Nullable(Of Boolean)
。将任何值与 Nothing 进行比较的结果都是 Nothing,Nothing 和“Not Nothing”都不是“True”,因此您总是会在 Else 子句中结束。
参见 MSDN page for nullable value types in VB更多细节。特别是,如果您寻找“比较可空类型”,您会找到一个与您的情况非常相似的示例(带解释)。特别是:
When the value of a Boolean variable or expression is Nothing, it is neither true nor false.
关于c# - VB.NET 奇怪的 IF 语句行为,总是其他,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10564438/