Inconsistent Equals(object) and GetHashCode()¶
ID: cs/inconsistent-equals-and-gethashcode
Kind: problem
Security severity:
Severity: warning
Precision: medium
Tags:
- reliability
- maintainability
- external/cwe/cwe-581
Query suites:
- csharp-security-and-quality.qls
Click to see the query in the CodeQL repository
A class that overrides only one of Equals(object)
and GetHashCode()
is likely to violate the contract of the GetHashCode()
method. The contract requires that GetHashCode()
gives the same integer result for any two equal objects. Not enforcing this property may cause unexpected results when storing and retrieving objects of such a class in a hashing data structure.
Recommendation¶
Provide an implementation of the missing method that is consistent with the present method.
Example¶
In the following example, the class Bad
overrides Equals(object)
but not GetHashCode()
.
using System;
class Bad
{
private int id;
public Bad(int Id) { this.id = Id; }
public override bool Equals(object other)
{
if (other is Bad b && b.GetType() == typeof(Bad))
return this.id == b.id;
return false;
}
}
In the revised example, the class Good
overrides both Equals(object)
and GetHashCode()
.
using System;
class Good
{
private int id;
public Good(int Id) { this.id = Id; }
public override bool Equals(object other)
{
if (other is Good b && b.GetType() == typeof(Good))
return this.id == b.id;
return false;
}
public override int GetHashCode() => id;
}
References¶
MSDN: Object.Equals Method (Object), Object.GetHashCode Method.
Common Weakness Enumeration: CWE-581.