Generate Equality Members
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.
JetBrains Rider 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.
Before generation | After generation |
---|---|
class Circle
{
/// <summary>Radius of the circle</summary>
int _radius;
/// <summary>Center point of the circle</summary>
Point _center;
/// <summary>Description of the circle</summary>
string _description;
}
|
class Circle
{
protected bool Equals(Circle other)
{
return _radius == other._radius
&& _center.Equals(other._center)
&& _description == other._description;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((Circle) obj);
}
public override int GetHashCode()
{
return HashCode.Combine(_radius, _center, _description);
}
/// <summary>Radius of the circle</summary>
int _radius;
/// <summary>Center point of the circle</summary>
Point _center;
/// <summary>Description of the circle</summary>
string _description;
}
|
Generate overrides for equality members
In the editor, place the caret at the type name or within a type at the line where you want to insert overrides for equality members. If the caret is on the type name, the generated code will be added in the beginning of the type declaration.
Press Alt+Insert or choose
from the main menu. Alternatively, you can press Control+Shift+A, start typing the command name in the popup, and then choose it there.In the Generate popup, select Equality Members.
In the Generate dialog that appears, select fields to be compared in the
Equals()
method and to be included in the value generation in theGetHashCode()
method.If there are no fields/properties in your type or you do not select any of them, JetBrains Rider, depending on your settings, throws new
NotImplementedException()
, returns default value, or puts code that will not compile in the body of the generated methods.Optionally, use the following controls in the dialog:
Fields can be null — appears if there are any nullable fields or properties in your type. By default, this checkbox is selected, meaning that JetBrains Rider will generate null checks for selected fields. You can clear this checkbox if you do not need null checks.
String comparison — appears if there are any string fields in your type. JetBrains Rider will generate
string.Compare(string, string, StringComparison)
for the selected strings, and the selector lets you choose which value should be generated for the StringComparison argument.GetHashCode already exists and Equals already exists appear if the corresponding overrides already exist and let you choose whether to:
Replace the method if it already exists.
Put the newly generated method side by side with the existing one.
Skip generating a new method altogether.
Use 'System.HashCode' to implement 'GetHashcode' — if this checkbox is selected, JetBrains Rider will use the
Combine
from System.HashCode (which uses xxHash32) in the generatedGetHashcode
implementation. Otherwise, JetBrains Rider will generate its own custom implementation.Overload equality operators — generates custom equality and inequality operators (that is
>==
and!=
) for the current type.Implement IEquatable<T> interface implements a type-specific
Equals()
method.Comparand type check — allows you to choose how the
Equals()
method will compare object types:Type exactly same as this
Type exactly as the owner type
Type is of the owner type or a subtype
Click OK to complete the wizard.
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.