ReSharper 2016.1 Help

Generating Equality Members

ReSharper | Edit | Generate Code | Equality Members
Alt+Insert | Equality Members

The implementation of equality methods (i.e. Equals() and GetHashCode()) as well as equality operators (i.e. == and !=) in the Object class guarantees reference equality. In a type you create (which ultimately derives from Object and thus implements reference equality by default), you may want to implement value equality for objects of this type and use the hashcode as a unique object identifier for hashing purposes. In this case, you need to override equality methods and operators for your type.

ReSharper allows you to automate these routines with the Generate equality members command.

In the example below, this command is used to generate Equals() and GetHashCode() methods based on _radius and _center fields.

Before generationAfter generation
class Circle { int _radius; Point _center; }
class Circle { int _radius; Point _center; protected bool Equals(Circle other) { return _radius == other._radius && _center.Equals(other._center); } public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) return false; if (ReferenceEquals(this, obj)) return true; if (obj.GetType() != this.GetType()) return false; return Equals((Circle) obj); } public override int GetHashCode() { unchecked { return (_radius*397) ^ _center.GetHashCode(); } } }

To generate equality members for your type

  1. In the editor, set the caret within a type, at the line where you want to insert overrides for equality members.
  2. Press Alt+Insert or choose ReSharper | Edit | Generate Codeā€¦ in the main menu .
  3. In the Generate pop-up menu, select Equality Members.
  4. In the Generate dialog box that appears, select fields to be compared within the Equals() method and to be included into the value generation in the GetHashCode() method.

    Generating equality members

    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:

    • Overload equality operators generates custom equality and inequality operators (i.e. >== and !=) for the current type.
    • Implement IEquatable<T> interface implements a type-specific Equals() method.
    • GetHashCode already exists and Equals already exists appear if the corresponding overrides already exist and let you indicate whether to:
      • Replace the corresponding override if it already exists.
      • Put the newly generated override side by side with the existing override.
      • Skip generating a new override altogether.
    • Comparand type check allows you to choose how the Equals() method will compare object types:
      • Type exactly same as this
      • Type exactly as the owner type
      • Type is of the owner type or a subtype
  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

You can also generate the two overrides by choosing Overriding Members in the Generate menu, but in that case they will both return base methods.

As an alternative to generating equality members within your type, you can generate equality comparer class for your type.

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 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