0

Why is it required on some operators Eg the comparison operators.

<   <=   >   >=   =  <>

You have to also implement their complement also?

<  >=

What is restrict your object implementing just

<

if that has specific meaning to it, and

>= 

doesn't

Note: The workaround I've is to stick the Obsolete attribute on the unwanted operator.

3
  • How would x < y have a specific meaning, but y >= x not, unless you're overloading operators to have a meaning very different than their intuitive meaning? Commented May 17, 2012 at 1:18
  • You could implement bound check more naturally Lower < Value < Upper Commented May 17, 2012 at 1:42
  • You're allow to overload just the plus operator. with your logic it suggest to me you should also implement the minus operator also. Commented May 17, 2012 at 1:44

2 Answers 2

4

Operator overloading in .Net (at least for C# and VB) is intentionally less flexible than operator overloading in C++.

One reason that Java went so far as to remove overloading entirely is that too many developers abused overloading to do clever-looking things that confused other developers and broke core assumptions of generic algorithms that expect to be able to, say, reasonably compare two instances of a user type.

While .Net brought back some flexibility, the designers were careful to only allow overloading of certain operators with the intent that they only be used for their customary meanings.

The requirement to implement comparison operators symmetrically is a nod to that intention.

If you want to do more aggressive operator overloading, I'd encourage you to consider using F#, where you have access to a much broader palette of symbols with which to draw clever operator symbols.

Sign up to request clarification or add additional context in comments.

Comments

0

Presumably because if you overload one side of an operator, you are implementing some custom rule, and the default operator would not match your rule, and thus produce odd behaviours.

For example, if your definition of 'int >' was 'return (x > 5);' (for some reason...) and you didn't overlaod the inverse rule for the 'int <' operator, then;

4 > 1 would be false (by your rule), but 4 < 1 would also be false (by the default rule).

Which is illogical, Jim.

1 Comment

inverses are '> <--> <= >= <--> < == <--> != ' It isn't restricted to return only a bool it can return (I think) any type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.