diff --git a/docs/fundamentals/code-analysis/quality-rules/ca2226.md b/docs/fundamentals/code-analysis/quality-rules/ca2226.md index 189a0b134c2c6..609709d65bb97 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca2226.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca2226.md @@ -10,6 +10,8 @@ helpviewer_keywords: - "CA2226" author: gewarren ms.author: gewarren +dev_langs: +- CSharp --- # CA2226: Operators should have symmetrical overloads @@ -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 struct 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. diff --git a/docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca2226.cs b/docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca2226.cs new file mode 100644 index 0000000000000..3fadff3f258c0 --- /dev/null +++ b/docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca2226.cs @@ -0,0 +1,75 @@ +using System; + +namespace ca2226 +{ + // + // 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); + } + // +} + +namespace ca2226_2 +{ + // + // 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); + } + // +}