Generate Equality Comparer

Last modified: 25 September 2024

IEqualityComparer<T> is a generic .NET interface that allows implementing customized equality comparison for collections.

Creating a comparer class for your type is an alternative to creating Equals() and GetHashCode() methods for the type. The generated comparer class will implement the IEqualityComparer<T> interface and provide custom Equals() and GetHashCode()) methods.

ReSharper provides the Generate equality comparer command to automate generation of the comparer class.

In the example below, this command is used to generate the comparer class based on _radius and _center fields.

Alternatively, you can write an empty comparer class that implements IEqualityComparer<T>, and then pick the corresponding quick-fix from the menu:

ReSharper: Generate equality comparer quick-fix

This feature is supported in the following languages and technologies:

The instructions and examples given here address the use of the feature in C#. For more information about other languages, refer to corresponding topics in the Languages and frameworks section.