Search code examples
c#unit-testingrhino-mocksrhinorhino-mocks-3.5

What's the exact syntax to concat 2 constraints in Rhino Mocks framework?


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?


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;
    }