0

Hey Everyone I thought that this code would be valid in any case because it is casted to bool

parameter.Request?.InnerData?.Any() == false

Instead this code is casted to type bool?

Can tell me someone why? (this is main question) Is there better way to check this instead of something like this?

var isThereInnerData = parameter.Request?.InnerData?.Any();
if (isThereSegments == null || isThereSegments == false)
  • 3
    Because once `Request` is `null` or `Request.InnerData` is `null`, the entire expression will evaluate to `null`. Since it can return either `null` or `bool` obviously the compiler infers it as `Nullable` (`bool?`). – haim770 Dec 13 '16 at 09:05
  • 1
    Insteadof `if (isThereSegments == null || isThereSegments == false)` you could just do `if (isThereSegments == true)` unless you need to write code for both scenarios as seen here ► [Best way to check for nullable bool in a condition expression (if ...)](http://stackoverflow.com/questions/2673918/best-way-to-check-for-nullable-bool-in-a-condition-expression-if) – Nope Dec 13 '16 at 09:06
  • I would imagine it makes sense for the compiler to return you a `bool?` since it is inherently uncertain if a Safe Navigation Operator will even be able to return a value – Wim Ombelets Dec 13 '16 at 09:07

3 Answers3

2

With a null-conditional operator, null propagates. You can't end up with a non-nullable type if you use the null-conditional operator - this is the same behaviour in languages where null operations are "safe" by default (SQL, Objective-C, ...). So the result is default(bool?) if parameter.Request is null, or parameter.Request.InnerData is null. Otherwise, you get true or false, but by using the null-conditional operator you already assume the result can in fact be null, so the compiler must accomodate that. Since bool can't be null, it is changed into a nullable bool (bool?).

The solution is simple - think about what logical value a null should have, and use that:

if (parameter.Request?.InnerData?.Any() ?? true)

In this case, a null value is interpreted as true, but you can also use false if you want. Alternatively, instead of the null-coalescing operator, you can use GetValueOrDefault, whatever feels better for you.

Luaan
  • 62,244
  • 7
  • 97
  • 116
1

Here is what C# reference says about ?. operator:

x?.y – null conditional member access. Returns null if the left hand operand is null.

Since at compile time the compiler doesn't knows whether the expression will evaluate to a non-null value, the compiler infers the type as Nullable<bool>

Since you are chaining the ?. operator this line from msdn documentation is relevant too:

The null-condition operators are short-circuiting. If one operation in a chain of conditional member access and index operation returns null, then the rest of the chain’s execution stops. Other operations with lower precedence in the expression continue. For example, E in the following always executes, and the ?? and == operations execute.

bashrc
  • 4,725
  • 1
  • 22
  • 49
0

bool is a value type and per default cannot be compared with null. C# converts your return value to bool? to give you the option to support both.

silkfire
  • 24,585
  • 15
  • 82
  • 105