ReSharper 2016.1 Help

Generating Equality Comparer

ReSharper | Edit | Generate Code | Equality Comparer
Alt+Insert | 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 generationAfter 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; } } }

To generate equality comparer for your type

  1. In the editor, set the caret within a type at the line where you want to insert the comparer class.
  2. Press Alt+Insert or choose ReSharper | Edit | Generate Codeā€¦ in the main menu .
  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 with ReSharper

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

    Optionally, use the following controls:

    • Expose via static property generates an instance of the comparer class and the corresponding static property.
    • 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 commmon code generation preferences on the Code Editing | Members Generation page of ReSharper options

This feature is supported in the following languages/technologies:

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

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

See Also

Last modified: 19 August 2016