Skip to content

Latest commit

 

History

History
83 lines (67 loc) · 3.33 KB

File metadata and controls

83 lines (67 loc) · 3.33 KB
title ms.date ms.topic f1_keywords helpviewer_keywords ms.assetid author ms.author manager dev_langs ms.workload
CA1013: Overload operator equals on overloading add and subtract
11/04/2016
reference
OverrideOperatorEqualsOnOverridingAddAndSubtract
OverrideOperatorEqualsOnOverloadingAddAndSubtract
CA1013
OverloadOperatorEqualsOnOverloadingAddAndSubtract
OverrideOperatorEqualsOnOverloadingAddAndSubtract
OverrideOperatorEqualsOnOverridingAddAndSubtract
CA1013
OverloadOperatorEqualsOnOverloadingAddAndSubtract
5bd28d68-c179-49ff-af47-5250b8b18a10
mikejo5000
mikejo
jillfra
CSharp
VB
multiple

CA1013: Overload operator equals on overloading add and subtract

Item Value
CheckId CA1013
Category Microsoft.Design
Breaking change Non-breaking

Cause

A public or protected type implements the addition or subtraction operators without implementing the equality operator.

Rule description

When instances of a type can be combined by using operations such as addition and subtraction, you should almost always define equality to return true for any two instances that have the same constituent values.

You cannot use the default equality operator in an overloaded implementation of the equality operator. Doing so will cause a stack overflow. To implement the equality operator, use the Object.Equals method in your implementation. See the following example.

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);

How to fix violations

To fix a violation of this rule, implement the equality operator so that it is mathematically consistent with the addition and subtraction operators.

When to suppress warnings

It is safe to suppress a warning from this rule when the default implementation of the equality operator provides the correct behavior for the type.

Example

The following example defines a type (BadAddableType) that violates this rule. This type should implement the equality operator to make any two instances that have the same field values test true for equality. The type GoodAddableType shows the corrected implementation. Note that this type also implements the inequality operator and overrides xref:System.Object.Equals%2A to satisfy other rules. A complete implementation would also implement xref:System.Object.GetHashCode%2A.

[!code-csharpFxCop.Design.AddAndSubtract#1]

Example

The following example tests for equality by using instances of the types that were previously defined in this topic to illustrate the default and correct behavior for the equality operator.

[!code-csharpFxCop.Design.TestAddAndSubtract#1]

This example produces the following output:

Bad type:  {2,2} {2,2} are equal? No
Good type: {3,3} {3,3} are equal? Yes
Good type: {3,3} {3,3} are == ?   Yes
Bad type:  {2,2} {9,9} are equal? No
Good type: {3,3} {9,9} are == ?   No

See also