Overriding Equality Operators

I 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) .

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

Does it end at some point? Or did I wander into the endless trail of requirements just because I want to overload == and != ?

+7
source share
2 answers

Does it end at some point?

Yes, once you implement GetHashCode , it will end. Eric Lippert spoke about his importance . All I can do is invite you to read and trust him :-)

+10
source

Yes, this will end if you override GetHashCode .

When implementing equality operators, and even more so Equals , the programmer must provide an implementation for delivering a custom hash code for this type.

See this link on MSDN for more details.

+1
source

All Articles