This class overrides only one of Equals()
and GetHashCode()
.
.NET objects are expected to obey a number of invariants related to equality. One of these invariants is that equal objects must have equal hashcodes. In other words, if a.Equals(b) == true
then a.GetHashCode() == b.GetHashCode()
.
Failure to uphold this invariant is likely to cause trouble if objects of this class are stored in a collection. If the objects of the class in question are used as a key in a Hashtable or if they are inserted into a Dictionary, it is critical that equal objects have equal hashcodes.
Example 1: The following class overrides Equals()
but not GetHashCode()
.
public class Halfway() {
public override boolean Equals(object obj) {
...
}
}
[1] Standards Mapping - Common Weakness Enumeration - (CWE) CWE ID 581
[2] MSDN Library: Equals Method (Object) Microsoft Corporation
[3] MSDN Library: GetHashCode Method (Object) Microsoft Corporation