I’ve tried these two blocks but it returns the same value. I do not describe more, I simply just show the code:
Dim f As Nullable(Of Integer)
If f = 1 Then
Console.WriteLine("Equal")
Else
Console.WriteLine("Not Equal")
End If
It prompts me “Not Equal”
I just add a NOT and I excpect to get the NOT answer, but I got the same as above!!!
Dim f As Nullable(Of Integer)
If Not f = 1 Then
Console.WriteLine("Equal")
Else
Console.WriteLine("Not Equal")
End If
It works in C# correctly…
TL;DR: It works correctly according to the behaviour which is specified for VB, which isn’t the same behaviour specified for C#.
The comparison “f = 1” yields a
Nullable(Of Boolean)in VB. The result of comparing any value with Nothing is Nothing, and neither Nothing nor “Not Nothing” is “True”, so you’ll always end up in the Else clause.See the MSDN page for nullable value types in VB for more details. In particular, if you look for “Comparing Nullable Types” you’ll find an example (with explanation) which is very similar to your situation. In particular: