SoFunction
Updated on 2025-03-06

Implementation of rewrite Equals method

Application scenario:

Many standard methods are used to compare methods, for example

Suppose in some scenarios, we hope that when referring to the type to determine "equal", we do not look at whether the address is the same, but whether certain attributes are the same. (For example, if the ID ID is one, it is considered the same person)

The rewriting method is shown in the following example:

Main{
    List<People> nList = new List<People> { new People( 1 ), new People( 2 ), new People( 3 ) };
    People onePeople = new People( 1 );
    ( onePeople );
 
}
 
 
class People
{
    public People( int nID )
    {
        ID = nID;
    }
    int ID;
 
    public override bool Equals( object obj )
    {
        return Equals( obj as People );
    }
 
    bool Equals( People other )
    {
        return other != null 
            && ID == ;
    }
 
}

. It is best to overide the GetHashCode method again:
(7 and 13 are just common methods, multiply the quality to ensure that the hash code is unique), you can also add ^ calculation

public override int GetHashCode()
{
    int hash =13;
    hash = (hash * 7) + ID== null ? 0 : ();
}

The reason is:

It is to determine whether it points to the same address
2. Each object will have a unique HashCode
Once the Equal method is override, but the GetHashCode method does not override, the GetHashCode method will cause two objects that are judged to be the same (judged using Equal), but the hash values ​​are different.
Inherited, where HashCode is used (such as the key in Dictionary), two identical objects may be repeatedly added to Dictionary.

When do I need to rewrite the Equals() method?

Reference type:

The instance version of the Equals() method should be overridden only when the semantics defined by the reference type needs to be modified. If a type needs to adopt value semantics instead of reference semantics (or rather than comparisons based on object content rather than object identity), then the instance version of the () method should be rewrite for this type.

Reference types generally do not need to be overridden operator==().

Value type:

When creating a value type, the () method should always be rewrited for this type.
Because value types are inherited from classes, classes are compared by reflection by default, which is not efficient enough.

The default == operator in the value type will be compared by reflection by default, so the == operator should also be rewritten.

Notes when rewriting the Equals() method

The Equals() method must satisfy the three mathematical properties of the equivalent relationship: reflexivity, symmetry, and transitiveness.
The Equals() method should never throw an exception.
When overriding the Equals() method, the base type version needs to be called only if the base type Equals(object) is not provided by or .
When rewriting Equals(), you should also let this type implement the IEquatable<T> interface.
After overriding the Equals() method, you should usually override the GetHashCode() method at the same time.

Notes when rewriting the GetHashCode() method

If the Equals() method determines that two objects are equal, then the HashCode of the two objects must also be the same;
For any object, its HashCode must remain unchanged throughout its life cycle;
The HashCode calculation method should map its values ​​evenly to individual integers to avoid heaping.
A commonly used HashCode algorithm is to call the GetHashCode() method on each independent immutable field in the type, and perform an XOR (XOR) operation on the returned HashCode, and use the resulting final result as the HashCode of the object itself.

This is the end of this article about the implementation of the C# rewrite Equals method. For more related C# rewrite Equals content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!