0

Why does the 2nd case not compile? Is there a way to make this work by overloading or sth?

bool b1 = true;
bool? b2 = false;

if (b1) //does compile
{
    //do sth.
}

if (b2) //doesn't compile
{
    //do sth.
}

if (b2 == true) //does compile
{
    //do sth.
}
Byyo
  • 2,163
  • 4
  • 21
  • 35

5 Answers5

1

An if statement expects a boolean to resolve.

You can use the Value property

if(b2.Value)

Of course, you will probably want to check if its null too..

== true compiles because the == (equals) operator is evaluating whether one object is equivalent to another. That doesn't necessarily mean that the output will always be correct (I'm not saying it wont be either) but you should treat a Nullable<T> as a Nullable<T> and use the appropriate properties

Sayse
  • 42,633
  • 14
  • 77
  • 146
  • but i also can `b2 == true` instead of `b2.Value == true` – Byyo Aug 25 '15 at 06:59
  • @Byyo - I've updated my answer to explain that - (Read more about my update in this [Eric Lippert answer](http://stackoverflow.com/a/9013171/1324033)) – Sayse Aug 25 '15 at 07:01
1

The second case doesn't compile because the if statement expects a boolean expression, but b2 itself is not a boolean expression. It can also be null.

I usually go with your 3rd option (b2 == true). That will evaluate to true if it is not null and the value is true.

In other words, b2 == true is equivalent to b2 != null && b2.Value.

Eren Ersönmez
  • 38,383
  • 7
  • 71
  • 92
  • any why doesn't `if (b2)` the same as `if(b2 == true)` – Byyo Aug 25 '15 at 07:05
  • 1
    @Byyo because `b2 == true` is a boolean expression, `b2` is not. Think of it this way: the first one will _always_ return a `true` or `false`. The second one can return `true`, `false`, or `null`. And the if statement doesn't know what to do with a `null`. – Eren Ersönmez Aug 25 '15 at 07:07
0

Because b2 is not of type bool, it's bool?, which is equivalent to Nullable<bool>.

You should try the following:

if (b2.HasValue && b2.Value)
{
  // do sth
}
Nissim
  • 6,395
  • 5
  • 49
  • 74
0

The variable of b2 should be null able.If you check first should be ahead value of this variable then after to check true or false.

Mukesh Kalgude
  • 4,814
  • 2
  • 17
  • 32
0

The reason why the third example works is because of lifted operators. So:

For the equality operators

== !=

a lifted form of an operator exists if the operand types are both non-nullable value types and if the result type is bool. The lifted form is constructed by adding a single ? modifier to each operand type. The lifted operator considers two null values equal, and a null value unequal to any non-null value. If both operands are non-null, the lifted operator unwraps the operands and applies the underlying operator to produce the bool result.

So, that's why the third example works - because all of this machinery has been invented and it works for all nullable types.

Your second example could have been made to work but it would have required the compiler to perform special work for exactly one nullable type - bool?. It's simply not appropriate, in general, for every use of a nullable type to perform an implicit conversion back to the original value type.


(Quote above from section 7.3.7 of the language specification)

Community
  • 1
  • 1
Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448