我在Rhino源代码中看到以下代码:
// Summary:
// Or operator for constraints
public static AbstractConstraint operator |(AbstractConstraint c1, AbstractConstraint c2);
但在实践中,我可以使用|或||。
类似地,&和&以同样的方式工作。
为什么会发生这种情况?
这实际上是一个C#问题。不能直接重载条件逻辑运算符&&
和||
,为了重载它们,程序员必须重载&
、|
、true
和false
运算符,而这些运算符用于计算包含&&
和||
运算符的表达式。更多详细信息,请参阅本文。
关于Rhino Mocks,他选择以防止短路的方式实现true
和false
运算符,并使&&
和||
运算符等效于&
和|
,ILSpy输出:
public static bool operator false(AbstractConstraint c)
{
return false;
}
public static bool operator true(AbstractConstraint c)
{
return false;
}