GetHashCode
and Equals
if operators ==
and !=
are overloaded CS-W1031Equality operators are syntactically meant to mimic the Equals
method which must always be overridden along with GetHashCode
. Not doing so may produce inconsistent behavior when evaluating equal
ness and produces compiler warning CS0660
. It is therefore recommended that you override both the GetHashCode
and Equals
methods in this case.
class Foo
{
public static bool operator ==(Foo f1, Foo f2)
{
// ...
}
public static bool operator !=(Foo f1, Foo f2)
{
// ...
}
}
class Foo
{
public static bool operator ==(Foo f1, Foo f2)
{
// ...
}
public static bool operator !=(Foo f1, Foo f2)
{
// ...
}
public override int GetHashCode()
{
// ...
}
public override bool Equals(object? obj)
{
// ...
}
}