Most likely you did not redefine GetHashCode according to Equals .
The GetHashCode contract requires that if OBJ1.Equals(OBJ2) returns true, then OBJ1.GetHashCode() must return the same value as OBJ2.GetHashCode() .
IIRC, you will get a compiler error (or at least a warning) if you override Equals without overriding GetHashCode() .
Another possibility is that you are not really overridden Equals , but overloaded it by adding a new signature, for example
public bool Equals(Class1 other)
In general, to ensure a "natural" comparison of equality of values, you need to:
- Override Equals (Object)
- Override GetHashCode
- Strictly consider implementing
IEquatable<T> - Consider overload == and! =
source share