Generating Equality Comparer
Alt+Insert |
Equality Comparer
ReSharper_GenerateEqualityComparer
IEqualityComparer<T>
is a generic .NET interface that allows implementing customized equality comparison for collections.
Creating a comparer class for your type is an alternative to
creating Equals() and GetHashCode() methods
for the type. The generated comparer class will implement the
IEqualityComparer<T>
interface and provide custom
Equals()
and
GetHashCode()
) methods.
ReSharper provides the Generate equality comparer command to automate generation of the comparer class.
In the example below, this command is used to generate the comparer class based on
_radius
and
_center
fields.
To generate equality comparer for your type
- In the editor, set the caret within a type at the line where you want to insert the comparer class.
- Press Alt+Insert or choose in the main menu .
- In the Generate pop-up menu, select Equality Comparer.
-
In the Generate dialog that appears, select fields to be used in the comparer class.
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 generatedEquals()
andGetHashCode()
methods. You can configure the settings on the page of ReSharper options.Optionally, use the following controls:
-
Click
Finish
to complete the wizard.
You can also click Options to review or modify common code generation preferences on the page of ReSharper options.
This feature is supported in the following languages/technologies:
C# | VB.NET | C++ | HTML | ASPX | Razor | JavaScript | TypeScript | CSS | XML | XAML | RESX | Build Scripts | Protobuf | JSON |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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.