If the condition is zero

There is a lot of syntactic sugar with Nullable<T> like this:

 int? parsed to Nullable<int> int? x = null if (x != null) // Parsed to if (x.HasValue) x = 56; // Parsed to x.Value = 56; 

And further.

Why doesn't the if condition work with Nullable?

 if (x) {} 

It receives a Complier error message, cannot convert Nullable<bool> to bool .
Why is this not parsed for if (x.HasValue && x.Value == true) or something like that?

This is the most obvious use for a Nullable<bool>

+6
source share
10 answers

This is the most obvious use for a Nullable<bool>

Your β€œobvious” behavior leads to many non-obvious behavior.

If

 if(x) 

treated as false, when x is null, then what should happen to

 if(!x) 

? !x also null when x is null, and therefore it will also be considered false! Does it not seem strange to you that you cannot reverse the behavior of a conditional with inversion?

What about

 if (x | !x) 

Of course, this should always be true, but if x is null, then the whole expression is null and therefore false.

It is better to avoid these unobvious situations by simply making them illegal. C # - "make the user say what they mean unambiguously."

I believe VB has the behavior you want. You might consider switching to VB if that is what you like.

+19
source

Simply put, it does not compile because the specification says it should - the if condition requires a boolean expression, and an expression of type Nullable<bool> not a logical expression:

A logical expression is an expression that produces a result of type bool; either directly or by applying the true operator in certain contexts, as described below.

However, it’s easy to work:

 if (x.GetValueOrDefault()) { } 

Or perhaps clearer:

 if (x ?? false) { } 

The latter approach is useful, as it means that you can easily change the behavior if x is null, just changing it to x ?? true x ?? true

+9
source

It's not so obvious that null should be false. A value of zero means that the value is unknown or uninitialized. It is not known whether the value is true or false. Who says null should behave as false?

A more obvious use for a Nullable<bool> is to save something that may be true or false, but sometimes it does not matter or is unknown, in which case the value is null.

+5
source

I think that would be nice. That null evaluates to false does not seem natural. Especially in if+else statements. Therefore forcing the user to be explicit with: if(nb==true) and if(nb==false) is a good IMO idea.

MSDN says:

 bool? b = null; if (b) // Error CS0266. { } 

This is unacceptable because it is unclear what null means in the context of the conditional. Use bool? in the conditional expression, first check its HasValue property to make sure its value is not null, and then drop it on bool. See the bool section for more information. Should you roll on bool? with a null value, an InvalidOperationException exception will be thrown in the conditional test.

http://msdn.microsoft.com/en-us/library/bb384091.aspx

Some answers to my question Why are there no canceled short-circuit statements on `bool ??? also apply to these aspects

+3
source

Technically, this does not work, because Nullable<T> does not define a conversion operator for bool .

In fact, it is not supported for various reasons, including:

  • There are no implicit conversions to bool anywhere in C # - why should it be in Nullable<T> ?
  • Any conversion must be declared for each T to Nullable<T> - how is this possible if you can plug in your own type like T , which the compiler knows nothing about?
  • What would be the semantics of if(!x) ?
+2
source

This is the most obvious use for nullable.

Be careful with such statements. What seems obvious to you is not necessarily what is obvious to someone else.

Moreover,

 if(x) {} 

it would also be illegal if x was a reference type, so in the interest of consistency, the behavior should be the same for Nullables.

+2
source

You cannot do

 if (x) {...} 

for the same reason you cannot write

 int? a = null; var b = a + 1; 

A nullable boolean is not a boolean. In some cases, do you want to consider a null bool value? like truth, and sometimes you want to consider it false. Language design does not make this decision for you.

+1
source

Consider what happens in this case, then

 bool? nullBool = null; if (nullBool) { someCode(); } 

It would not be possible to indicate whether it entered senctence because the bool was null or false. This is probably undesirable.

+1
source

This is because a nullable bool cannot be explicitly converted to bool. This is for the same reason why you can do this:

 int x1 = 7; int? x2 = x1; 

but you cannot do this:

 int? x1 = 7; int x2 = x1; 

you should check if it contains a value. Implicitly conversion using types with a null value can only be performed when assigning a value, and not when using this value.

+1
source

Anything between the brackets in the if statement must evaluate to true or false. A nullable bool can evaluate to null, true, or false.

0
source

Source: https://habr.com/ru/post/906313/


All Articles