Skip to content

Gendarme.Rules.Design.ImplementEqualsAndGetHashCodeInPairRule(git)

Sebastien Pouliot edited this page Mar 2, 2011 · 1 revision

ImplementEqualsAndGetHashCodeInPairRule

Assembly: Gendarme.Rules.Design
Version: git

Description

This rule checks for types that either override the Equals(object) method without overriding GetHashCode() or override GetHashCode without overriding Equals. In order to work correctly types should always override these together.

Examples

Bad example (missing GetHashCode):

public class MissingGetHashCode {
    public override bool Equals (object obj)
    {
        return this == obj;
    }
}

Bad example (missing Equals):

public class MissingEquals {
    public override int GetHashCode ()
    {
        return 42;
    }
}

Good example:

public class Good {
    public override bool Equals (object obj)
    {
        return this == obj;
    }
    public override int GetHashCode ()
    {
        return 42;
    }
}

Source code

You can browse the latest source code of this rule on github.com

Clone this wiki locally