Skip to content

Gendarme.Rules.Design.OverrideEqualsMethodRule(git)

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

OverrideEqualsMethodRule

Assembly: Gendarme.Rules.Design
Version: git

Description

This rule warns when a type overloads the equality == operator but does not override the Object.Equals method.

Examples

Bad example:

class DoesNotOverrideEquals {
    public static bool operator == (DoesNotOverloadOperatorEquals a, DoesNotOverloadOperatorEquals b)
    {
        return true;
    }
}

Good example:

class OverridesEquals {
    public static bool operator == (OverridesEquals a, OverridesEquals b)
    {
        return true;
    }
    public override bool Equals (object obj)
    {
        OverridesEquals other = (obj as OverridesEquals);
        if (other == null) {
            return false;
        }
        return (this == other);
    }
}

Source code

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

Clone this wiki locally