Question

I see in the Rhino source code the following code:

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

But in practice, I can use either | or ||.

Similarly, both & and && work the same way.

Why is this happening?

Was it helpful?

Solution

This is actually a C# issue. You can't overload the conditional logical operators - && and || directly, in order to overload them the programmer has to overload the &,|, true and false operators, and those operators are used to evaluate an expression containing the && and || operators. More details in this article.

Concerning Rhino Mocks, he chose to implement the true and false operators in a way that prevents short circuiting and makes the && and || operators equivalent to & and |, ILSpy output:

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

public static bool operator true(AbstractConstraint c)
{
    return false;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top