I would like to have a reference on how to correctly implement a class that satisfies following requisites:
- implementing a logic for equality that depends from its content, i.e. its fields
- proven for data binding
To satisfy point 1, I am used to implement IEquatable<T>
,to override Object’s Equals
, GetHashCode
and ==
and !=
operators accordingly. Overriding also Object’s Equals
and GetHashCode
is important as stated for instance here and here.
However, GetHashCode
can return different values according to the fields of the class and I've recently discovered that for point 2 you need to have GetHashCode
never changing for a correct data binding in WinForms.
So, it seems I cannot have both the ability to override ==
operator to check for the equality of the class' content and the ability to bind to class properties. Which is the proper way to implement such a class also thinking that other users will access it?