Search code examples
c#operator-overloadingequalsgethashcode

Overriding Equality Operators


I've implemented a class that overloads the == and != operators.

This seems to work fine; however, I get the warning 'type' defines operator == or operator != but does not override Object.Equals(object o).

Okay, so I implemented Equals. But now I get the warning 'type' defines operator == or operator != but does not override Object.GetHashCode().

Does this end at some point? Or have I wandered into a never-ending trail of requirements just because I want to overload == and !=?


Solution

  • Does this end at some point?

    Yes, once you implement GetHashCode it will end. Eric Lippert has blogged about its importance. All I can do is suggest you to read and trust him :-)