Skip to content

Gendarme.Rules.BadPractice.EqualsShouldHandleNullArgRule(2.10)

Sebastien Pouliot edited this page Jan 22, 2011 · 2 revisions

EqualsShouldHandleNullArgRule

Assembly: Gendarme.Rules.BadPractice
Version: 2.10

Description

This rule ensures that Equals(object) methods return false when the object parameter is null.

Examples

Bad example:

public bool Equals (object obj)
{
    // this would throw a NullReferenceException instead of returning false
    return ToString ().Equals (obj.ToString ());
}

Good example:

public override bool Equals (object obj)
{
    if (obj == null) {
        return false;
    }
    return ToString ().Equals (obj.ToString ());
}
Clone this wiki locally