Why does the following code return false?
public static void Main()
{
bool? someCondition = true;
bool someConditionOverride = false;
bool? result = someCondition ?? someConditionOverride ? false : (bool?)null;
Console.WriteLine(result);
}
I calculated the result will be true, since someConditionnot null, but the ??operator will return true. However, it seems that the right operand is evaluated first, and the left side is simply ignored.
Adding parentheses eliminates the confusion:
bool? result = someCondition ?? (someConditionOverride ? false : (bool?)null)
And the result will be true. However, I am still wondering why in the first example the left side of the left side was ignored.
Artem source
share