DA0006: Override Equals() for value types

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Item Value
Rule Id DA0006
Category .NET Framework Usage
Profiiling methods Sampling
Message Override Equals and equality operator on value types.
Messge type Warning

Cause

Calls to the Equals method or the equality operators of a public value type are a significant proportion of the profiling data. Consider implementing a more efficient method.

Rule description

For value types, the inherited implementation of Equals uses the System.Reflection library and compares the contents of all fields in the type. Reflection is computationally expensive, and comparing every field for equality might be unnecessary. If you expect users to compare or sort instances or to 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.

For more information about how to override Equals and the equality operators, see Guidelines for Implementing Equals and the Equality Operator (==).

How to investigate a warning

For an example of implementing the Equals and equality operators, see the code analysis rule CA1815: Override equals and operator equals on value types