Today, I saw two students discussing the execution of the gethashcode method. It seems that their object equivalence judgment is quite complicated, and they want to write the same logic in gethashcode, then we will discuss the algorithms used to make the integers returned by gethashcode more unique.
In short, gethashcode is used:Try to make a preliminary judgment on the object at the fastest time. Of course, there is no specific requirement on the speed of time and the depth of judgment, as long as it is not extreme (for example, it is too time-consuming or the depth of judgment is too small ). So it's not necessary. gethashcode is too complicated!
Some people mistakenly think that the storage of the dictionary relies entirely on the gethashcode result. Obviously, this is not correct. How can the gethashcode returns only one int for all results?
In this example, the gethashcode of such a class returns the result of the remainder 2 of the integer data. (For example only, it is clear that the execution of this gethashcode is very efficient, but it is too deep)
Class
{
Public int ID {Get; private set ;}
Public A (int I)
{
Id = I;
}
Public override bool equals (Object OBJ)
{
Console. writeline ("equals ");
If (OBJ = NULL | GetType ()! = Obj. GetType ())
{
Return false;
}
Return id = (a) OBJ). ID;
}
// Return the result of the remainder 2
Public override int gethashcode ()
{
Console. writeline ("gethashcode ");
Return ID % 2;
}
}
Then run the Code:
VaR O1 = new A (1); // gethashcode returns 1
VaR O2 = new A (2); // gethashcode returns 0
VaR O3 = new A (3); // gethashcode returns 1
VaR DIC = new dictionary <A, Object> ();
Dic. Add (O1, 123 );
Console. writeline ("separator ");
Console. writeline (DIC. containskey (O2 ));
Console. writeline ("separator ");
Console. writeline (DIC. containskey (O3 ));
Program output:
Gethashcode
Delimiter
Gethashcode
False
Delimiter
Gethashcode
Equals
False
As you can see, when gethashcode can be used to identify the inequality, equals does not need to be called. When gethashcode returns the same result, the equals method is called to ensure that the object is equal. Therefore, it is still the same sentence: gethashcode does not have to clearly distinguish the object (besides, it is impossible. An int cannot represent all possible values ), equals is supported later. Gethashcode only needs to quickly judge the object.
Finally, you may be wondering why you don't have to use equals to get a gethashcode first? Since the equals method has to clarify whether the two objects are equal to or not, the efficiency may not be optimal (besides, the object. equals usually contains conversions of types. For details, refer to iequatable or iequalitycomparer. They support generics). gethashcode does not need to be absolutely clear about whether it is equal, so it can optimize the efficiency. To give a simple example, if two people are completely the same (in the same case, they are cloned), equals compares one cell to another, while gethashcode can determine the gender, appearance, sound ...... Quick judgment. Therefore, first use gethashcode to quickly identify many different people. Of course, if gethashcode returns true (when twins or clone humans are met), then use equals for thorough comparison.