Each implementation of Equals
must fulfil the following requirements:
x.Equals(x)
returns true
.x.Equals(y)
returns the same value as y.Equals(x)
.(x.Equals(y) && y.Equals(z))
returns true
, then x.Equals(z)
returns true
.x.Equals(y)
return the same value as long as the objects referenced by x and y are not modified.null
.x.Equals(null)
returns false
.Implementations of GetHashCode
:
Equals
: If two objects are equal (meaning that Equals
returns true), then GetHashCode
must return the same value for each of them.Equals
says false), there should be a high probability their hash codes are distinct. Perfect hashing is often not possible as there is a limited number of values to choose from.See: Guidelines for Overloading Equals() and Operator ==
Writing a good GetHashCode override