Override Equals on overloading operator equals
Override Equals on overloading operator equals
Microsoft docsDescription
The equality operator is intended to be a syntactically convenient way to access the functionality of the System.Object.Equals method. If you implement the equality operator, its logic must be identical to that of System.Object.Equals. This rule only applies to Visual Basic code. The C# compiler generates a separate warning, CS0660.
Cause
A public type implements the equality operator but doesn't override the System.Object.Equals method.
How to fix violations
To fix a violation of this rule, you should either remove the implementation of the equality operator, or override System.Object.Equals and have the two methods return the same values. If the equality operator doesn't introduce inconsistent behavior, you can fix the violation by providing an implementation of System.Object.Equals that calls the System.Object.Equals method in the base class.
Example
#pragma warning disable CA2224
// The code that's violating the rule is on this line.
#pragma warning restore CA2224When to suppress
It's safe to suppress a warning from this rule if the equality operator returns the same value as the inherited implementation of System.Object.Equals. The examples in this article include a type that could safely suppress a warning from this rule.