问题
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.
回答1:
"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.
来源:https://stackoverflow.com/questions/44417468/c-sharp-coalesce-operator