ReSharper
 
Get ReSharper
Get your hands on the new features ahead of the release by joining the Early Access Program for ReSharper 2025.1! Learn more

Generate Equality Members

Last modified: 11 February 2024

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 more information about other languages, refer to corresponding topics in the Languages and frameworks section.