Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# coalesce operator

Why the following code returns false?

public static void Main()
{
    bool? someCondition = true;
    bool someConditionOverride = false;


    bool? result = someCondition ?? someConditionOverride ? false : (bool?)null;

    Console.WriteLine(result);
}

I was exprecting the result will be true, since someCondition is not null and ?? operator will return true. However looks like right operand is calculated first and the left part is simply ignored.

Adding brackets fix the confusion:

bool? result = someCondition ?? (someConditionOverride ? false : (bool?)null)

And the result will be true. However I am still curious why left part had been ignored in the first example.

like image 911
Artem Avatar asked Dec 03 '22 13:12

Artem


1 Answers

"the left part is simply ignored"? How likely do you really think that is?

Operator precedence dictates that your first version is parsed like so:

bool? result = (someCondition ?? someConditionOverride) 
                   ? false 
                   : (bool?)null;

someCondition isn't null, and it is true. Therefore, this expression evaluates as true:

(someCondition ?? someConditionOverride) 

So we get the ? branch, and the whole expression returns false, just like you told it to.

Adding the brackets that you added completely changes the meaning of the expression. It fixes your confusion, in a limited sense, by bringing the actual meaning of the expression in line with your original intent; but the compiler is never confused. In C#, compiler confuses you.

To reduce my own confusion, I never rely on operator precedence. I parenthesize everything. If I had designed the language, the grammar would require it (unless Eric comes by and tells me why that's actually not such a good idea after all, for some reason that will make perfect sense to me once Eric explains it).

UPDATE: Prediction validated: Eric came by and said that too many people would find it ridiculous to have to parenthesize a + b * c, and if you allow that, there's no reasonable way to define the exceptions. Well, if they won't tolerate being forced to parenthesize everything, they won't, however much I think they should.

like image 187
15ee8f99-57ff-4f92-890c-b56153 Avatar answered Jan 01 '23 17:01

15ee8f99-57ff-4f92-890c-b56153