or 表达式中的顺序是否有保证
Is order guaranteed in an or expression
我有这样的表达:
EqualByComparer comparer;
if (ListEqualByComparer.TryGetOrCreate(x, y, out comparer) ||
EnumerableEqualByComparer.TryGetOrCreate(x, y, out comparer))
{
return comparer.Equals(x, y, compareItem, settings, referencePairs);
}
ListEqualByComparer.TryGetOrCreate
是否总是在 EnumerableEqualByComparer.TryGetOrCreate
之前被调用?
Will ListEqualByComparer.TryGetOrCreate
always be called before EnumerableEqualByComparer.TryGetOrCreate
?
是的,并且由于||
短路,只有在第一次调用时才会进行第二次调用returns false
.
来自 C# 5 规范,第 7.12.1 节:
When the operands of &&
or ||
are of type bool
, or when the operands are of types that do not define an applicable operator &
or operator |
, but do define implicit conversions to bool
, the operation is processed as follows:
[...]
The operation x || y
is evaluated as x ? true : y
. In other words, x
is first evaluated and converted to type bool
. Then, if x
is true, the result of the operation is true
. Otherwise, y
is evaluated and converted to type bool
, and this becomes the result of the operation.
是 - 详细信息在 documentation。
仅当第一个条件为假时才评估第二个条件
来自 C# 参考资料(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.
是的,订单有保证。 MSDN 状态:
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.
我有这样的表达:
EqualByComparer comparer;
if (ListEqualByComparer.TryGetOrCreate(x, y, out comparer) ||
EnumerableEqualByComparer.TryGetOrCreate(x, y, out comparer))
{
return comparer.Equals(x, y, compareItem, settings, referencePairs);
}
ListEqualByComparer.TryGetOrCreate
是否总是在 EnumerableEqualByComparer.TryGetOrCreate
之前被调用?
Will
ListEqualByComparer.TryGetOrCreate
always be called beforeEnumerableEqualByComparer.TryGetOrCreate
?
是的,并且由于||
短路,只有在第一次调用时才会进行第二次调用returns false
.
来自 C# 5 规范,第 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.
是 - 详细信息在 documentation。
仅当第一个条件为假时才评估第二个条件
来自 C# 参考资料(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.
是的,订单有保证。 MSDN 状态:
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.