在 Rhino Mocks 框架中连接 2 个约束的确切语法是什么?



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

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

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

类似地,&&amp以同样的方式工作。

为什么会发生这种情况?

这实际上是一个C#问题。不能直接重载条件逻辑运算符&&||,为了重载它们,程序员必须重载&|truefalse运算符,而这些运算符用于计算包含&&||运算符的表达式。更多详细信息,请参阅本文。

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

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

最新更新