ReSharper
 
Get ReSharper
You are viewing the documentation for an earlier version of ReSharper.

Generate Equality Members

Last modified: 21 July 2022

ReSharper | Edit | Generate Code | Equality Members

Alt+Insert | Equality Members (ReSharper_GenerateEqualityMembers)

The implementation of equality methods (that is Equals() and GetHashCode()) as well as equality operators (that is == 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, _center, and _description fields.

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 and technologies:

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.