Override equals and operator equals on value types
Override equals and operator equals on value types
Microsoft docsDescription
For non-blittable value types, the inherited implementation of System.Object.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 System.Object.Equals. If your programming language supports operator overloading, you should also provide an implementation of the equality and inequality operators.
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.
How to fix violations
To fix a violation of this rule, provide an implementation of System.Object.Equals. If you can, implement the equality operator.
Example
#pragma warning disable CA1815
// The code that's violating the rule is on this line.
#pragma warning restore CA1815When to suppress
It's safe to suppress a warning from this rule if instances of the value type will not be compared to each other.