Override Equals when implementing IEquatable
Override Equals when implementing IEquatable
Microsoft docsDescription
A type implementing System.IEquatable1 interface indicates that it supports comparing two instances of the type for equality. You should also override the base class implementations of System.Object.Equals and System.Object.GetHashCode methods so that their behavior is consistent with that of the System.IEquatable1.Equals implementation. For more information, see Notes to implementers.
Your System.Object.Equals implementation should return results that are consistent with System.IEquatable`1.Equals implementation.
Cause
A type implements System.IEquatable`1, but does not override System.Object.Equals method.
How to fix violations
To fix a violation, override System.Object.Equals and implement it by invoking the System.IEquatable`1.Equals implementation. For example, the following two code snippets show a violation of the rule and how to fix it:
Example
using System;
public struct S : IEquatable<S>
{
private readonly int _value;
public S(int f)
{
_value = f;
}
public bool Equals(S other)
=> _value == other._value;
}When to suppress
Do not suppress violations of this rule.