CA1815: Override equals and operator equals on value types

Property Value
Rule ID CA1815
Title Override equals and operator equals on value types
Category Performance
Fix is breaking or non-breaking Non-breaking
Enabled by default in .NET 8 No

Cause

A value type does not override System.Object.Equals or does not implement the equality operator (==). This rule does not check enumerations.

By default, this rule only looks at externally visible types, but this is configurable.

Rule description

For non-blittable value types, the inherited implementation of Equals uses the System.Reflection library to compare the contents of all fields. Reflection is computationally expensive, and comparing every field for equality might be unnecessary. If you expect users to compare or sort instances, or use them as hash table keys, your value type should implement Equals. If your programming language supports operator overloading, you should also provide an implementation of the equality and inequality operators.

How to fix violations

To fix a violation of this rule, provide an implementation of Equals. If you can, implement the equality operator.

When to suppress warnings

It's safe to suppress a warning from this rule if instances of the value type will not be compared to each other.

Suppress a warning

If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

#pragma warning disable CA1815
// The code that's violating the rule is on this line.
#pragma warning restore CA1815

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

[*.{cs,vb}]
dotnet_diagnostic.CA1815.severity = none

For more information, see How to suppress code analysis warnings.

Configure code to analyze

Use the following option to configure which parts of your codebase to run this rule on.

You can configure this option for just this rule, for all rules it applies to, or for all rules in this category (Performance) that it applies to. For more information, see Code quality rule configuration options.

Include specific API surfaces

You can configure which parts of your codebase to run this rule on, based on their accessibility. For example, to specify that the rule should run only against the non-public API surface, add the following key-value pair to an .editorconfig file in your project:

dotnet_code_quality.CAXXXX.api_surface = private, internal

Example

The following code shows a structure (value type) that violates this rule:

// Violates this rule
public struct Point
{
    public Point(int x, int y)
    {
        X = x;
        Y = y;
    }

    public int X { get; }

    public int Y { get; }
}

The following code fixes the previous violation by overriding System.ValueType.Equals and implementing the equality operators (== and !=):

public struct Point : IEquatable<Point>
{
    public Point(int x, int y)
    {
        X = x;
        Y = y;
    }

    public int X { get; }

    public int Y { get; }

    public override int GetHashCode()
    {
        return X ^ Y;
    }

    public override bool Equals(object? obj)
    {
        if (!(obj is Point))
            return false;

        return Equals((Point)obj);
    }

    public bool Equals(Point other)
    {
        if (X != other.X)
            return false;

        return Y == other.Y;
    }

    public static bool operator ==(Point point1, Point point2)
    {
        return point1.Equals(point2);
    }

    public static bool operator !=(Point point1, Point point2)
    {
        return !point1.Equals(point2);
    }
}

See also