CA1013:重载加法方法和减法方法时重载相等运算符

类型名

OverloadOperatorEqualsOnOverloadingAddAndSubtract

CheckId

CA1013

类别

Microsoft.Design

是否重大更改

非重大更改

原因

公共或受保护类型实现加或减运算符时没有实现相等运算符。

规则说明

当类型的实例可以使用运算(例如加或减)连接起来时,应当始终定义相等,以便为有相同的组成值的两个实例返回 true。

不能在相等运算符的重载实现中使用默认相等运算符。 这样做将会导致堆栈溢出。 要实现相等运算符,请在实现中使用 Object.Equals 方法。 请参见下面的示例。

If (Object.ReferenceEquals(left, Nothing)) Then
    Return Object.ReferenceEquals(right, Nothing)
Else
    Return left.Equals(right)
End If
if (Object.ReferenceEquals(left, null)) 
    return Object.ReferenceEquals(right, null);
return left.Equals(right);

如何解决冲突

要修复与该规则的冲突,请实现相等运算符,使它与加和减运算符在数学上一致。

何时禁止显示警告

当相等运算符的默认实现为类型提供正确行为时,则可以安全地禁止显示此规则发出的警告。

示例

下面的示例定义一个与该规则冲突的类型 (BadAddableType)。 此类型应当实现相等运算符,使任意两个有相同字段值的实例测试为 true 来说明它们相等。 类型 GoodAddableType 演示了更正的实现。 请注意,此类型还实现不等运算符并重写 Equals 以满足其他规则。 一个完整的实现还将实现 GetHashCode

using System;

namespace DesignLibrary
{
   public class BadAddableType
   {
      private int a, b;
      public BadAddableType(int a, int b)
      {
         this.a = a;
         this.b = b;
      }
      // Violates rule: OverrideOperatorEqualsOnOverridingAddAndSubtract.
      public static BadAddableType operator +(BadAddableType a, BadAddableType b)
      {
         return new BadAddableType(a.a + b.a, a.b + b.b);
      }
      // Violates rule: OverrideOperatorEqualsOnOverridingAddAndSubtract.
      public static BadAddableType operator -(BadAddableType a, BadAddableType b)
      {
         return new BadAddableType(a.a - b.a, a.b - b.b);
      }
      public override string ToString()
      {
         return String.Format("{{{0},{1}}}", a, b);
      }
   }

   public class GoodAddableType
   {
      private int a, b;
      public GoodAddableType(int a, int b)
      {
         this.a = a;
         this.b = b;
      }
      // Satisfies rule: OverrideOperatorEqualsOnOverridingAddAndSubtract.
      public static bool operator ==(GoodAddableType a, GoodAddableType b)
      {
         return (a.a == b.a && a.b == b.b);
      }

      // If you implement ==, you must implement !=.
      public static bool operator !=(GoodAddableType a, GoodAddableType b)
      {
         return !(a==b);
      }

      // Equals should be consistent with operator ==.
      public override bool Equals(Object obj)
      {
         GoodAddableType good = obj as GoodAddableType;
         if (obj == null)
            return false;

        return this == good;
      }

      public static GoodAddableType operator +(GoodAddableType a, GoodAddableType b)
      {
         return new GoodAddableType(a.a + b.a, a.b + b.b);
      }

      public static GoodAddableType operator -(GoodAddableType a, GoodAddableType b)
      {
         return new GoodAddableType(a.a - b.a, a.b - b.b);
      }
      public override string ToString()
      {
         return String.Format("{{{0},{1}}}", a, b);
      }
   }
}

下面的示例使用本主题前面定义的类型的实例测试是否相等,以阐释相等运算符的默认行为和正确行为。

using System;

namespace DesignLibrary
{
    public class TestAddableTypes
    {
       public static void Main()
       {
          BadAddableType a = new BadAddableType(2,2);
          BadAddableType b = new BadAddableType(2,2);
          BadAddableType x = new BadAddableType(9,9);
          GoodAddableType c = new GoodAddableType(3,3);
          GoodAddableType d = new GoodAddableType(3,3);
          GoodAddableType y = new GoodAddableType(9,9);

          Console.WriteLine("Bad type:  {0} {1} are equal? {2}", a,b, a.Equals(b)? "Yes":"No");
          Console.WriteLine("Good type: {0} {1} are equal? {2}", c,d, c.Equals(d)? "Yes":"No");
          Console.WriteLine("Good type: {0} {1} are == ?   {2}", c,d, c==d? "Yes":"No");
          Console.WriteLine("Bad type:  {0} {1} are equal? {2}", a,x, a.Equals(x)? "Yes":"No");
          Console.WriteLine("Good type: {0} {1} are == ?   {2}", c,y, c==y? "Yes":"No");
       }
    }
}

该示例产生下面的输出。

              

请参见

参考

Guidelines for Implementing Equals and the Equality Operator (==)