I want to provide GetHashCode and Equals methods for my components. My
understanding of these methods is that if two of the same type of components
return the same value from GetHashCode, then they must return true from the
Equals method if compared.
In the GetHasCode documentation it is very clearly stated that "The hash
function must return exactly the same value regardless of any changes that
are made to the object." Clearly the Equals method, if it is not dependent
on the object's actual reference value of a reference type but on property
or field values of the object instead, is dependent on changes to the
object. Therefore I do not understand how the Equals value, which depends on
changes to the object, can be in sync with the GetHashCode value, which
depends on the immutability of some value. Can anyone enlighten me on these
subjects ?
Mattias Sj?gren - 29 Dec 2003 20:03 GMT
Edward,
>My
>understanding of these methods is that if two of the same type of components
>return the same value from GetHashCode, then they must return true from the
>Equals method if compared.
No, two objects can return the same hash code without being considered
equal. However, the reverse is true. If one.Equals(another) == true
then they should return the same hash code.
Mattias

Signature
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.