Why can I compare nullable boolean values in C# like this:
bool? test = true;
if (test==true)
//do somethign
but not like this:
bool? test = true;
if (test)
//do somethign
Why can I compare nullable boolean values in C# like this:
bool? test = true;
if (test==true)
//do somethign
but not like this:
bool? test = true;
if (test)
//do somethign
The if
statement in C# can only take a bool
parameter.
Nullable<bool>
is not the same as bool
, and null
is neither true
nor false
.
If you know your bool?
has a value, you can use:
if (test.Value)
//do something