ReSharper 2023.3 Help

Generate Relational Members

ReSharper helps you automatically overload relational operators and implementations of related interfaces using selected fields and properties. By default, ReSharper will create an implementation of IComparable<T>, and optionally, operators >, <, and as well as an implementation of IComparable are generated too.

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

Before generation

After generation

class Person { private int _birthYear; private string _name; }
class Person : IComparable<Person>, IComparable { private int _birthYear; private string _name; public int CompareTo(Person other) { if (ReferenceEquals(this, other)) return 0; if (ReferenceEquals(null, other)) return 1; var birthYearComparison = _birthYear.CompareTo(other._birthYear); if (birthYearComparison != 0) return birthYearComparison; return string.Compare(_name, other._name, StringComparison.Ordinal); } // Optionally: Implementation of IComparable and relational operators public int CompareTo(object obj) { if (ReferenceEquals(null, obj)) return 1; if (ReferenceEquals(this, obj)) return 0; if (!(obj is Person)) throw new ArgumentException($"Object must be of type {nameof(Person)}"); return CompareTo((Person) obj); } public static bool operator <(Person left, Person right) { return Comparer<Person>.Default.Compare(left, right) < 0; } public static bool operator >(Person left, Person right) { return Comparer<Person>.Default.Compare(left, right) > 0; } public static bool operator <=(Person left, Person right) { return Comparer<Person>.Default.Compare(left, right) <= 0; } public static bool operator >=(Person left, Person right) { return Comparer<Person>.Default.Compare(left, right) >= 0; } }

Generate relational members

  1. In the editor, place the caret at the type name or within a type at the line where you want to insert relational members. 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 Relational Members.

  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 relational members 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.

    • CompareTo already exists — appears if an implementation of CompareTo() already exists and lets you choose whether to:

      • Replace the method if it already exists.

      • Put the newly generated method side by side with the existing one.

      • Skip generating a new method altogether.

    • Implement IComparable interface — tick this checkbox to generate an implementation of IComparable interface in addition to IComparable<T>, that is generate the CompareTo(object) method in addition to CompareTo(T).

  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 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 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 ReSharper by language section.

Last modified: 21 March 2024