title | description | ms.date | f1_keywords | helpviewer_keywords | author | ms.author | ||||
---|---|---|---|---|---|---|---|---|---|---|
CA1815: Override equals and operator equals on value types (code analysis) |
Learn about code analysis rule CA1815: Override equals and operator equals on value types |
03/11/2019 |
|
|
gewarren |
gewarren |
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 |
A value type does not override xref:System.Object.Equals%2A?displayProperty=fullName 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.
For non-blittable value types, the inherited implementation of xref:System.Object.Equals%2A uses the xref: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 xref:System.Object.Equals%2A. If your programming language supports operator overloading, you should also provide an implementation of the equality and inequality operators.
To fix a violation of this rule, provide an implementation of xref:System.Object.Equals%2A. If you can, implement the equality operator.
It's safe to suppress a warning from this rule if instances of the value type will not be compared to each other.
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.
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.
[!INCLUDEapi-surface]
The following code shows a structure (value type) that violates this rule:
:::code language="csharp" source="snippets/csharp/all-rules/ca1815.cs" id="snippet1":::
The following code fixes the previous violation by overriding xref:System.ValueType.Equals%2A?displayProperty=fullName and implementing the equality operators (==
and !=
):
:::code language="csharp" source="snippets/csharp/all-rules/ca1815.cs" id="snippet2":::
- CA2231: Overload operator equals on overriding ValueType.Equals
- CA2226: Operators should have symmetrical overloads
- xref:System.Object.Equals%2A?displayProperty=fullName