1

I know null-coalescing operator returns the left hand operand if it is not null, but I hadn't seen anyone using it in an if-statement like:

if (car.IsSold ?? false)
 ...

I'm guessing it means if car.IsSold is not null then proceed with the if statement, otherwise jump out of the statement. Is that right? if yes, why shouldn't the programmer use

if (car.IsSold)
...

which does the exact same thing? I'd appreciate if someone clears this up for me.

1
  • Comments are not for extended discussion; this conversation has been moved to chat. Commented Jan 6, 2023 at 11:13

2 Answers 2

2

which does the exact same thing?

No it doesn't. Because a bool? (or Nullable<bool>) and a bool are not the exact same thing.

An if condition must resolve to a bool. But a bool? might resolve to null at runtime. So the compiler won't allow that. You have to provide logic in the condition which will still return a bool in the event that the value in question is null. Probably the simplest way to do that is the null-coalescing operator and a default literal.

Sign up to request clarification or add additional context in comments.

4 Comments

the reason the compiler does not allow that isn't bc it might resolve to null.. it's bc that there is no implicit cast
@Adrian: Two ways of saying the same thing, really. The compiler is attempting to interpret the condition as a bool because that's what it requires. If the result is a bool, the necessity is met. If it isn't, the compiler attempts to find an implicit cast from whatever the condition does resolve to. Ultimately it needs to become a bool.
no, you're arguing that it won't allow that bc it might resolve to null. If you explicitly cast to bool it still might, but it will compile
@Adrian: Which would result in a runtime error instead of a compile time error, which the compiler is specifically meant to avoid. But still it's semantically the same thing that I've described. The explicit cast resolves to a bool (at least as far as the compiler is concerned, since it's really the developer telling the compiler "don't worry about this, I promise it'll be a bool"), which is what an if requires. The explicit cast may fail at runtime, but that's a different problem. We're both saying the same thing, and we're both right, we're just describing it differently to the OP.
2

Assuming car.IsSold is nullable:

if (car.IsSold)

does not even compile since bool? does not implicitly cast to bool


The explicit cast

 if ((bool)car.IsSold)

will throw an exception if car.IsSold is null


Using the null-coalescing operator

if (car.IsSold ?? false)

would work in this case, among other solutions.

4 Comments

Or simply use car.IsSold.GetValueOrDefault() which returns false if IsSold is null.
@SebastianHofmann In this case I find the null-coalescing operator way more readable, but that's just a personal preference.
@SebastianHofmann actually i think the ?? version is better, its less typing and the default is explicitly given
@da_berni yeah, I'd also prefer the ?? operator, I was just adding an option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.