CA2225: Operator overloads have named alternates

 

For the latest documentation on Visual Studio 2017, see Visual Studio 2017 Documentation.

For the latest documentation on Visual Studio 2017, see CA2225: Operator overloads have named alternates on docs.microsoft.com.

TypeNameOperatorOverloadsHaveNamedAlternates
CheckIdCA2225
CategoryMicrosoft.Usage
Breaking ChangeNon Breaking

An operator overload was detected, and the expected named alternative method was not found.

Operator overloading allows the use of symbols to represent computations for a type. For example, a type that overloads the plus symbol (+) for addition would typically have an alternative member named 'Add'. The named alternative member provides access to the same functionality as the operator, and is provided for developers who program in languages that do not support overloaded operators.

This rule examines the operators listed in the following table.

C#Visual BasicC++Alternate name
+ (binary)++ (binary)Add
+=+=+=Add
&And&BitwiseAnd
&=And=&=BitwiseAnd
|Or|BitwiseOr
|=Or=|=BitwiseOr
--N/A--Decrement
///Divide
/=/=/=Divide
=====Equals
^Xor^Xor
^=Xor=^=Xor
>>>Compare
>=>=>=Compare
++N/A++Increment
<>!=Equals
<<<<<<LeftShift
<<=<<=<<=LeftShift
<<<Compare
<=<=<=Compare
&&N/A&&LogicalAnd
||N/A||LogicalOr
!N/A!LogicalNot
%Mod%Mod or Remainder
%=N/A%=Mod
* (binary)**Multiply
*=N/A*=Multiply
~Not~OnesComplement
>>>>>>RightShift
=N/A>>=RightShift
- (binary)- (binary)- (binary)Subtract
-=N/A-=Subtract
trueIsTrueN/AIsTrue (Property)
- (unary)N/A-Negate
+ (unary)N/A+Plus
falseIsFalseFalseIsTrue (Property)

N/A == Cannot be overloaded in the selected language.

The rule also checks implicit and explicit cast operators in a type (SomeType) by checking for methods named ToSomeType and FromSomeType.

In C#, when a binary operator is overloaded, the corresponding assignment operator, if any, is also implicitly overloaded.

To fix a violation of this rule, implement the alternative method for the operator; name it using the recommended alternative name.

Do not suppress a warning from this rule if you are implementing a shared library. Applications can ignore a warning from this rule.

The following example defines a structure that violates this rule. To correct the example, add a public Add(int x, int y) method to the structure.

using System;

namespace UsageLibrary
{
    public struct Point
    {
        private int x,y;
        
        public Point(int x, int y)
        {
            this.x = x;
            this.y = y;
        }

        public override string ToString()
        {
            return String.Format("({0},{1})",x,y);
        }

        // Violates rule: OperatorOverloadsHaveNamedAlternates.
        public static Point operator+(Point a, Point b)
        { 
            return new Point(a.x + b.x, a.y + b.y);
        }

        public int X {get {return x;}}
        public int Y {get {return x;}}
    }
}

CA1046: Do not overload operator equals on reference types

CA2226: Operators should have symmetrical overloads

CA2224: Override equals on overloading operator equals

CA2218: Override GetHashCode on overriding Equals

CA2231: Overload operator equals on overriding ValueType.Equals

Show: