.NET – Compare integers and integers? The result is a Boolean value? Not a Boolean

I just write some code to compare the id of an integer with the id of an integer? For example:

Dim id As Integer = 1
Dim nullId As Integer? = Nothing
Dim areEqual As Boolean
areEqual = nullId = id

When I try to compile the code, I get a compiler error:

Option Strict On disallows implicit conversions from'Boolean?' to 'Boolean'.

Although I can easily solve this problem, I hope someone can explain what is happening in the compiler to issue this warning.

This is one of the quirks of nullable types. NULL (generally speaking) means “don’t know”. Compare known and unknown unknown results (because you don’t Know if they are the same).

It is the same as the nullable type in .NET. Compare an integer? Will using Integer result in boolean values? , Because you may get True, False or “don’t know”.

I just write some code to compare the id of an integer with the id of an integer? For example:

Dim id As Integer = 1
Dim nullId As Integer? = Nothing
Dim areEqual As Boolean
areEqual = nullId = id

When I try to compile the code, I get a compiler error:

Option Strict On disallows implicit conversions from'Boolean?' to 'Boolean'.

Although I can easily solve this problem, I hope someone can explain what is happening in the compiler to issue this warning.

This is one of the quirks of nullable types. NULL (generally speaking) means “don’t know”. Compare known and unknown unknown results (because you don’t know if they are the same).

It is the same as the nullable type in .NET. Compare an integer? Will using Integer result in boolean values? , Because you may get True, False or “don’t know”.

Leave a Comment

Your email address will not be published.