1

I'm writing a class library to solve non-linear equations using newton's method. I stumbled across operator-overloading and thought about overloading the ==-Operator. Something like expression1 == expression2 returns the solution as a Constant, which is basically a wrapper-class of System.Double:

public static Constant operator ==(Derivable d1, Derivable d2)
{
    return d1.Equal(d2);
}

Although it compiles perfectly fine and works, I was asking myself if it would ever be a reasonable design-choice to overload the ==-Operator to return something else than the equality of two objects as a bool, especially because you also have to overload the !=-Operator. Is this bad practice and should I rather just use my method Equal?

Damien Flury
  • 769
  • 10
  • 23
  • 4
    That pretty much screams bad practice. If you want to return a double, define your own method, create an abstract class that inherits from double and implement it there, or create an extension method, but overloading operators to do something they are not meant to do.. just picture a developer implementing the library and getting unexpected behaviour on equals... – DevionNL Nov 21 '17 at 09:26
  • @DevionNL `Double` is a `sealed struct`, you cannot inherit from it... – Damien Flury Nov 21 '17 at 09:27
  • 5
    I'd say this is an opinion based question... But personally I'd find it very confusing if when I perform `==` on two objects it won't be a `bool`. At the very least it should be something implicitly converted to bool – Gilad Green Nov 21 '17 at 09:27
  • 4
    Personally I would advise against this. – Matthew Watson Nov 21 '17 at 09:28
  • 1
    @DamienFlury Forgot about that, ok, so class with internal double value, implement everything - same result ;) – DevionNL Nov 21 '17 at 09:30
  • 1
    _Mathematicians....always trying to over-complicate things_ –  Nov 21 '17 at 09:31
  • There is precedent: In Microsoft Solver Foundation there is a class `Term` that has overridden the == operator. It takes 2 Terms as input and returns another Term. But in general, `==` would return a boolean. – Dennis_E Nov 21 '17 at 09:53

1 Answers1

3

As a fellow developer, I would suggest not overriding the == Operator (C# Reference).

For predefined value types, the equality operator (==) returns true if the values of its operands are equal, false otherwise. For reference types other than string, == returns true if its two operands refer to the same object. For the string type, == compares the values of the strings.

I can't imagine a scenario where you would want to override this behavior. If you are working with classes then you could override the Object.Equals Method (Object).

If you are working with other developers this could be very confusing.

jegtugado
  • 5,081
  • 1
  • 12
  • 35
  • 3
    I would also **not** advise to overload the `Equals` method: 1. it won't work, OP wants to return a `Constant`, not a `bool`. 2. The argument "don't override a method to do something completely different than it was meant to" applies to `Equals`.as well as to `==`. OP does not want to _compare_ the two expressions, but to _solve the equation `a = b`_. So `Equals` is the wrong method. – René Vogt Nov 21 '17 at 09:33