-3

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
4

1 回答 1

0

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
于 2013-03-30T12:40:00.250 回答