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.
See more on this question at Stackoverflow