2
bool? nullableVar;
if(nullableVar) 

gives me an error but

if(nullableVar==true) 

evaluates fine.

Not sure I follow on why that is given that if the bool wasn't nullable would evaluate fine?

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
Sali
  • 87
  • 1
  • 6
  • This is a duplicate of http://stackoverflow.com/questions/447408/why-do-nullable-bools-not-allow-ifnullable-but-do-allow-ifnullable-true which has a great, detailed answer – Robert Levy Feb 10 '11 at 16:18

7 Answers7

4

Writing if (nullableVar) will try to implicitly convert the bool? into a bool. (Beause ifs require bools)

Since there is no implicit conversion from T? to T, you can't do that.

Note that you can also write

if (nullableVar ?? false)
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Thanks for the response on this. Was not aware of the inability to convert. Cheers! – Sali Feb 10 '11 at 16:30
4

The condition expression of an if has to be implicitly convertible to bool. Nullable<bool> is not convertible to bool, but the second expression is already of type bool, so it's okay. Looking at the IL, I believe the second expression is really being compiled to:

if (nullableVar.GetValueOrDefault())
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • it's a good question though, why doesn't Nullable have an implicit converter to T? - aside from the "what happens if it's null" I suppose... – Massif Feb 10 '11 at 16:19
  • It's convertible to `bool`, but only explicitly. – Adam Robinson Feb 10 '11 at 16:19
  • 2
    @Massif: Implicit conversions shouldn't lose data or throw exceptions. It's hard to see how an implicit conversion from three possible states (true/false/null) to two possible states (true/false) can cope with that. – Jon Skeet Feb 10 '11 at 16:20
  • Thanks for the comprehensive coverage on this Jon. I wasn't thinking along the lines of tri-state. Moreso as to why it wasn't regarded as a check if value is true condition. – Sali Feb 10 '11 at 16:28
  • @Massif: `T?` is just C# shorthand for `Nullable`. – Adam Robinson Feb 10 '11 at 16:31
  • @Adam: I believe the "?" in Massif's comment wasn't meant to be part of the code - it's just the end of a question :) – Jon Skeet Feb 10 '11 at 16:42
  • @Jon: Clearly (and somewhat unfortunately) my brain prioritizes C# syntax over basic english syntax. – Adam Robinson Feb 10 '11 at 16:44
  • I shall be more careful with my punctuation in future... :) – Massif Feb 10 '11 at 21:23
1

Nullable<T> is only explicitly convertible to T. In your case, your bool? is only explicitly convertible to bool. What you're trying to do here is an implicit conversion (an explicit conversion requires a cast to the desired type, whereas implicit does not).

Your comparison is an expression that results in a bool, which is why it's allowed.

Adam Robinson
  • 182,639
  • 35
  • 285
  • 343
0

An if clause needs to be of a boolean type. Null is not a boolean type. By making the comparison, you are creating an expression with a result that is boolean.

unholysampler
  • 17,141
  • 7
  • 47
  • 64
0

a nullable bool can have 3 different values: true, false or null

bool? myNullableBool;
if (myNullableBool == false)
...

a non nullable bool can have only 2 values: true or false

bool myBool;
if (myBool)
...
zov
  • 4,092
  • 1
  • 16
  • 19
0

Because == uses an Equals overload call, whereas if() expects the expression to be a bool (or as Jon Skeet says implicitly convertible to bool).

Kieren Johnstone
  • 41,277
  • 16
  • 94
  • 144
0

A bool? (such as nullableVar) can have three different values: true, false, or null.

if(nullableVar) is ambiguous when nullableVar is null.

if(nullableVar==true) is clearly false when nullableVar is null.

C. Dragon 76
  • 9,882
  • 9
  • 34
  • 41