Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/fundamentals/code-analysis/quality-rules/ca2226.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ helpviewer_keywords:
- "CA2226"
author: gewarren
ms.author: gewarren
dev_langs:
- CSharp
---
# CA2226: Operators should have symmetrical overloads

Expand Down Expand Up @@ -37,6 +39,16 @@ The C# compiler issues an error for violations of this rule.

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

## Example

The following example shows a sturct that violates this rule.

:::code language="csharp" source="snippets/csharp/all-rules/ca2226.cs" id="snippet1":::

The following example fixes the violation by adding the `!=`, `>` and `<=` operators.

:::code language="csharp" source="snippets/csharp/all-rules/ca2226.cs" id="snippet2":::

## When to suppress warnings

Do not suppress a warning from this rule. If you do, your type will not work in a manner that's consistent with .NET.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;

namespace ca2226
{
//<snippet1>
// This struct violates the rule.
public struct Point
{
public Point(int x, int y)
{
X = x;
Y = y;
}

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

public override int GetHashCode() => HashCode.Combine(X, Y);

// CA2226: Since 'Point' redefines operator '==', it should also redefine operator '!='
public static bool operator ==(Point left, Point right)
=> left.X == right.X && left.Y == right.Y;

// CA2226: Since 'Point' redefines operator '<', it should also redefine operator '>'
public static bool operator <(Point left, Point right)
=> left.DistanceFromOrigin() < right.DistanceFromOrigin();

// CA2226: Since 'Point' redefines operator '>=', it should also redefine operator '<='
public static bool operator >=(Point left, Point right)
=> left.DistanceFromOrigin() >= right.DistanceFromOrigin();

private double DistanceFromOrigin() => Math.Sqrt(X * X + Y * Y);
}
//</snippet1>
}

namespace ca2226_2
{
//<snippet2>
// This struct satisfies the rule.
public struct Point
{
public Point(int x, int y)
{
X = x;
Y = y;
}

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

public override int GetHashCode() => HashCode.Combine(X, Y);

public static bool operator ==(Point left, Point right)
=> left.X == right.X && left.Y == right.Y;

public static bool operator !=(Point left, Point right)
=> !(left == right);

public static bool operator <(Point left, Point right)
=> left.DistanceFromOrigin() < right.DistanceFromOrigin();

public static bool operator >(Point left, Point right)
=> left.DistanceFromOrigin() > right.DistanceFromOrigin();

public static bool operator >=(Point left, Point right)
=> left.DistanceFromOrigin() >= right.DistanceFromOrigin();

public static bool operator <=(Point left, Point right)
=> left.DistanceFromOrigin() <= right.DistanceFromOrigin();

private double DistanceFromOrigin() => Math.Sqrt(X * X + Y * Y);
}
//</snippet2>
}