DA0010: Expensive GetHashCode

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 DA0010
Category .NET Framework Usage
Profiling methods Sampling

.NET Memory
Message GetHashCode functions should be cheap and not allocate any memory. Reduce complexity of hash code function if possible.
Message type Warning

Cause

Calls to the GetHashCode method of the type are a significant proportion of the profiling data or the method allocates memory.

Rule description

Hashing is a technique for rapidly locating a particular item in a large collection. Because hash tables can be large and have to support very high rates of access, hash tables should be efficient. An implication of this requirement is that GetHashCode methods in the .NET Framework should not allocate memory. Allocating memory increases the load on the garbage collector and exposes the method to potential delays if it becomes necessary to run garbage collection as a result of the allocation request.

How to fix violations

Reduce the complexity of the method.