I have an expression like this:
EqualByComparer comparer;
if (ListEqualByComparer.TryGetOrCreate(x, y, out comparer) ||
EnumerableEqualByComparer.TryGetOrCreate(x, y, out comparer))
{
return comparer.Equals(x, y, compareItem, settings, referencePairs);
}
Will ListEqualByComparer.TryGetOrCreate
always be called before EnumerableEqualByComparer.TryGetOrCreate
?
Will
ListEqualByComparer.TryGetOrCreate
always be called beforeEnumerableEqualByComparer.TryGetOrCreate
?
Yes, and as ||
is short-circuiting, the second call will only be made if the first call returns false
.
From the C# 5 specification, section 7.12.1:
When the operands of
&&
or||
are of typebool
, or when the operands are of types that do not define an applicableoperator &
oroperator |
, but do define implicit conversions tobool
, the operation is processed as follows:[...]
The operation
x || y
is evaluated asx ? true : y
. In other words,x
is first evaluated and converted to typebool
. Then, ifx
is true, the result of the operation istrue
. Otherwise,y
is evaluated and converted to typebool
, and this becomes the result of the operation.
Yes - details are in the documentation.
The second condition is only evaluated if the first is false
From C# Reference (link):
The conditional-OR operator (||) performs a logical-OR of its bool operands. If the first operand evaluates to true, the second operand isn't evaluated. If the first operand evaluates to false, the second operator determines whether the OR expression as a whole evaluates to true or false.
Yes, the order is guaranteed. MSDN states:
Logical operators also guarantee evaluation of their operands from left to right. However, they evaluate the smallest number of operands needed to determine the result of the expression. This is called "short-circuit" evaluation. Thus, some operands of the expression may not be evaluated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With