ReSharper Web Help

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 within the type. The generated comparer class will implement the IEqualityComparer<T> interface and provide custom Equals() and GetHashCode()) methods.

ReSharper allows you to generate the comparer class with the Generate equality comparer feature.

Tip

You can set up common code generation options in the ReSharper | Options | Code Editing | Members Generation.

To generate equality comparer for your type
  1. In the editor, position the caret within a type implementation at the line where you want to insert the comparer class.
  2. On the main menu, choose ReSharper | Edit | Generate Code, or press Alt+Insert.
  3. In the Generate pop-up menu, select Equality comparer.
  4. In the Generate dialog box that appears, select fields to be used in the comparer class.
    Generating equality comparer

    Tip

    If you do not select any fields, the default body defined in the options is generated. Depending on your settings, ReSharper throws new NotImplementedException(), returns default value, or puts code that will not compile in the body of the generated Equals() and GetHashCode() methods. For more information, see ReSharper | Options | Code Editing | Members Generation.

    Optionally, use the following controls:
    • Fields can be null includes nullity check for every participating type member into the expression that the overridden GetHashCode() method returns (to configure nullity check for specific members, click Advanced).
    • Expose via static property generates an instance of the comparer class and the corresponding static property.
    • Comparer name prefix that will be used in the name of the generated comparer class.
  5. Click Finish to complete the wizard.

Tip

To bind any code generation command to its own shortcut, go to Tools | Options | Environment | Keyboard and look for commands starting with ReSharper_Generate.


This feature is supported in the following languages/technologies:

C# VB.NET HTML ASPX Razor JavaScript TypeScript CSS XML XAML RESX Build Scripts
feature_available feature_available

Tip

The instructions and examples given here address the use of the feature in C#. For details specific to other languages, see the corresponding topics in the ReSharper by Language section.