ReSharper 2017.1 Help

Generating Relational Comparer

ReSharper | Edit | Generate Code | Relational Comparer
Alt+Insert | Relational Comparer
ReSharper_GenerateRelationalComparer

ReSharper helps you create a nested comparer class derived from Comparer<T> using selected fields and properties. In addition to choosing a string comparison algorithm and generating nullability checks, you can opt for generating a static property that will expose the comparer class to consumers.

In the example below, this command is used to generate a relational comparer that takes into account int _birthYear and string _name fields when to compare objects of the Person class.

Before generationAfter generation
class Person { private int _birthYear; private string _name; }
class Person { private int _birthYear; private string _name; private sealed class BirthYearNameRelationalComparer : Comparer<Person> { public override int Compare(Person x, Person y) { if (ReferenceEquals(x, y)) return 0; if (ReferenceEquals(null, y)) return 1; if (ReferenceEquals(null, x)) return -1; var birthYearComparison = x._birthYear.CompareTo(y._birthYear); if (birthYearComparison != 0) return birthYearComparison; return string.Compare(x._name, y._name, StringComparison.Ordinal); } } public static Comparer<Person> BirthYearNameComparer { get; } = new BirthYearNameRelationalComparer(); }

To generate a relational comparer class

  1. In the editor, set the caret on the type name or within a type at the line where you want to insert a relational comparer class. If the caret is on the type name, generated code will be added in the beginning of the type declaration.
  2. Press Alt+Insert or choose ReSharper | Edit | Generate Codeā€¦ in the main menu.
  3. In the Generate pop-up menu, select Relational Comparer.
  4. In the Generate dialog that appears, you will see a list of properties and fields. Select the members that you want to use for type comparison.
    Generating a relational comparer class 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 check box is selected meaning that ReSharper will generate nullability checks for selected fields. You can clear this check box if you do not need nullability 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.

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 is available Feature is available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not 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: 4 July 2017