0

我在 Rhino 源代码中看到以下代码:

// Summary:
//     Or operator for constraints
public static AbstractConstraint operator |(AbstractConstraint c1, AbstractConstraint c2);

但在实践中,我可以使用| || .

同样,&&&的工作方式相同。

为什么会这样?

4

1 回答 1

1

这实际上是一个 C# 问题。您不能重载条件逻辑运算符 -为了重载它们,程序员必须直接重载、&&和运算符,这些运算符用于评估包含and运算符的表达式。更多细节在这篇文章中||&|truefalse&&||

关于 Rhino Mocks,他选择以一种防止短路true的方式实现and运算符,并使and运算符等效于and ,ILSpy 输出:false&&||&|

public static bool operator false(AbstractConstraint c)
{
    return false;
}

public static bool operator true(AbstractConstraint c)
{
    return false;
}
于 2013-12-06T08:55:31.587 回答