The default implementation of equality methods (i.e. Equals() and GetHashCode()) as well as equality operators (i.e. >== and !=) in .NET supports reference equality for reference types. If you derive your type from a reference type, 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 feature.
You can set up common code generation options in the ReSharper | Options | Code Editing | Members Generation.
- In the editor, position the caret within a type, at the line where you want to insert overrides for equality members.
- On the main menu, choose ReSharper | Edit | Generate Code, or press Alt+Insert.
- In the Generate pop-up menu, select Equality members.
Generate dialog box that appears, select fields to be compared within the
Equals() method and to be included into the value generation in the
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. For more information, see ReSharper | Options | Code Editing | Members Generation.
- Fields can be null includes nullity check for every participating type member into the expression that the overridden GetHashCode() method returns (to configure nullity check for specific members, click Advanced).
- 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
Do one of the following:
- To configure nullity options individually for specific members, click Advanced. After finishing this step, click Next.
- Click Finish to complete the wizard.
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.
To bind any code generation command to its own shortcut, go to Tools | Options | Environment | Keyboard and look for commands starting with ReSharper_Generate.
This feature is supported in the following languages/technologies:
The instructions and examples given here address the use of the feature in C#. For details specific to other languages, see the corresponding topics in the ReSharper by Language section.
- Generating Type Constructors
- Generating Properties
- Implementing/Overriding Members
- Generating Delegating Members
- Generating Equality Comparer
- Generating Partial Methods
- Generating Formatting Members