ReSharper 2024.3 Help

Generate Equality Comparer

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.

Before generation

After generation

class Circle { int _radius; Point _center; }
class Circle { int _radius; Point _center; private sealed class RadiusCenterEqualityComparer : IEqualityComparer<Circle> { public bool Equals(Circle x, Circle y) { if (ReferenceEquals(x, y)) return true; if (ReferenceEquals(x, null)) return false; if (ReferenceEquals(y, null)) return false; if (x.GetType() != y.GetType()) return false; return x._radius == y._radius && x._center.Equals(y._center); } public int GetHashCode(Circle obj) { unchecked { return (obj._radius*397) ^ obj._center.GetHashCode(); } } } private static readonly IEqualityComparer<Circle> RadiusCenterComparerInstance = new RadiusCenterEqualityComparer(); public static IEqualityComparer<Circle> RadiusCenterComparer { get { return RadiusCenterComparerInstance; } } }

Generate an equality comparer class

  1. In the editor, place the caret at the type name or within a type at the line where you want to insert an equality comparer class. If the caret is on the type name, the generated code will be added in the beginning of the type declaration.

  2. Press Alt+Insert or choose ReSharper | Edit | Generate Code… from the main menu. Alternatively, you can press Control+Shift+A, start typing the command name in the popup, and then choose it there.

  3. In the Generate popup, select Equality Comparer.

  4. In the Generate dialog that appears, select fields to be used in the comparer class.

    Generating equality comparer with ReSharper

    If there are no fields/properties in your type or you do not select any of them, ReSharper, depending on your settings, throws new NotImplementedException(), returns default value, or puts code that will not compile in the body of the generated methods. You can configure the settings on the Code Editing | Members Generation page of ReSharper options.

    Optionally, use the following controls in the dialog:

    • Fields can be null — appears if there are any nullable fields or properties in your type. By default, this checkbox is selected, meaning that ReSharper will generate null checks for selected fields. You can clear this checkbox if you do not need null checks.

    • String comparison — appears if there are any string fields in your type. ReSharper will generate string.Compare(string, string, StringComparison) for the selected strings, and the selector lets you choose which value should be generated for the StringComparison argument.

    • Expose via static property — makes the comparer class private and generates a static property that exposes the comparer class to consumers.

    • Comparer name prefix — allows you to specify a prefix that will be used in the name of the generated comparer class.

  5. Click Finish to complete the wizard.

    You can also click Options to review or modify common code generation preferences on the Code Editing | Members Generation page of ReSharper options.

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

ReSharper: Generate equality comparer quick-fix

This feature is supported in the following languages and technologies:

Language: C#

Language: VB.NET

Language: C++

Language: HTML

Language: ASP.NET

Language: Razor

Language: JavaScript

Language: TypeScript

Language: CSS

Language: XML

Language: XAML

Language: Resx

Language: Build Scripts

Language: Protobuf

Language: JSON

Feature is available in C#

Feature is available in Visual Basic

Feature is not available in C++

Feature is not available in HTML

Feature is not available in ASP.NET

Feature is not available in Razor

Feature is not available in JavaScript

Feature is not available in TypeScript

Feature is not available in CSS

Feature is not available in XML

Feature is not available in XAML

Feature is not available in Resource files

Feature is not available in build script files

Feature is not available in Protobuf

Feature is not available in JSON

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.

Last modified: 25 September 2024