Partilhar via


CA2226: Operators should have symmetrical overloads

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Item Value
TypeName OperatorsShouldHaveSymmetricalOverloads
CheckId CA2226
Category Microsoft.Usage
Breaking Change Non Breaking

Cause

A type implements the equality or inequality operator and does not implement the opposite operator.

Rule Description

There are no circumstances where either equality or inequality is applicable to instances of a type, and the opposite operator is undefined. Types typically implement the inequality operator by returning the negated value of the equality operator.

The C# compiler issues an error for violations of this rule.

How to Fix Violations

To fix a violation of this rule, implement both the equality and inequality operators, or remove the one that is present.

When to Suppress Warnings

Do not suppress a warning from this rule. Your type will not work in a manner that is consistent with the .NET Framework.

CA1046: Do not overload operator equals on reference types

CA2225: Operator overloads have named alternates

CA2224: Override equals on overloading operator equals

CA2218: Override GetHashCode on overriding Equals

CA2231: Overload operator equals on overriding ValueType.Equals