ReSharper Help  

Generating Equals and GetHashCode

With ReSharper's help you are able to quickly generate Equals() and GetHashCode() methods for a class.

To generate delegating members:

  1. Position the caret inside the class for which you want to generate Equals() and GetHashCode() methods.
  2. Press Alt + Ins or select ReSharper | Code | Generate in the main menu. The Generate popup menu is displayed:

     

     

  3. Select Equals and GetHashCode from the popup menu. The Equals and GetHashCode dialog appears:

  4. In the list, select the fields which you want to include in the Equals() comparison (in the example given we selected three public fields of different types). Click Continue. The following dialog appears:

  5. In the list, select the fields which you want to include in the GetHasCode() algorithm. Note that only those fields which were selected for the Equals() method are included in the list. Click Continue. The following dialog appears:

  6. In the list, select the fields which you can assume to never hold a null value. This will result in a shorter and simpler implementation of GetHashCode() method. In this particular example, a string value is available to select (but that is purely optional). Click Finish.

Example

The example used above starts with the following code:

After we go through the steps described above and click Finish, SalesItem class ends up looking like the following:

Note    Currently, if Equals() and/or GetHashCode() methods already exist in a class, the Generate Equals and GetHashCode command will still be available for use. However, after these methods are automatically generated, you may have to comment out the old methods to prevent compilation errors.

See Also

Advanced Editing Features | Automatic Code Generation