Generate Equals and GetHashCode method overrides in Visual Studio
This code generation applies to:
What: Lets you generate Equals and GetHashCode methods.
When: Generate these overrides when you have a type that should be compared by one or more fields, instead of by object location in memory.
If you are implementing a value type, you should consider overriding the Equals method to gain increased performance over the default implementation of the Equals method on ValueType.
If you are implementing a reference type, you should consider overriding the Equals method if your type looks like a base type, such as Point, String, BigNumber, and so on.
Override the GetHashCode method to allow a type to work correctly in a hash table. Read more guidance on equality operators.
Place your cursor somewhere on the line of your type declaration.
Do not double-click select the type name, or the menu option won't be available. Just place the cursor somewhere on the line.
Next, do one of the following:
Press Ctrl+. to trigger the Quick Actions and Refactorings menu.
Right-click and select the Quick Actions and Refactorings menu.
Click the icon that appears in the left margin.
Select Generate Equals(object) or Generate Equals and GetHashCode from the drop-down menu.
In the Pick members dialog box, select the members you want to generate the methods for:
You can also choose to generate operators from this dialog by using the checkbox near the bottom of the dialog.
GetHashCodemethods are generated with default implementations.