Common Operations for all objects: Object equivalence, unique, hash code, and clone.
6.1 adequacy and uniqueness of Objects
The equals method of system. object is: if two references point to the same object, true is returned; otherwise, false is returned.
6.1.1 The base class does not override the reference type of the object. Equals method to implement equals
First, judge whether two objects are null. Second, judge whether the two objects are of the same type. Then, convert the types to determine whether the fields of the two objects are equal. Finally, if you are happy, reload = and! = Operator.
The type conversion here makes it impossible to throw an exception because the types of the two objects have been compared before.
6.1.2 override the reference type Implementation of object. Equals for the base class
The judgment logic here is the same as that in the previous section. The only difference is to compare the fields defined in the base type.
6.1.3 implement the equals Method for Value Type
Value types are inherited from system. valuetype. The implementation of the valuetype equals method is as follows:
First, determine whether the object is null. Second, determine whether the two objects are of the same type. Then, obtain all the public and private instance fields of the object type and compare the fields;
Although the valuetype equals method is applicable in most cases, we implement the equals method by ourselves,ProgramHigher execution efficiency. Because the custom value type contains the reference type, it involves packing and unpacking during comparison.
6.1.4 equals method and = /! = Operator implementation Summary
The compiler provides the = /! = Overwrites the operator. For custom types, reload them slowly.
6.1.5 unique object recognition
The system. Object. referenceequals method is used to determine whether two references point to the same instance.
Int32 x = 5 ;
Console. writeline (object. referenceequals (x, x )) // False
Because X is packed into different objects twice.
Conclusion: It is easy to forget to rewrite the object equals method without having to practice multiple times. Considering the many details, you have to worry about it. Write it down, and write it next time based on the example.
6.2 object hash code
Why is there a hash code?
Because any object instance can be put into a hash set. The hash is stored as a key-value pair. The hash code is a key.
While rewriting the equals method, you must also override the gethashcode method.
The equals method and the gethashcode method are rewritten because the implementation of the system. Collections. hashtable type requires that any two equal objects must have the same hash code value.
Hash code for Type instancesAlgorithmWe should follow the following principles as much as possible:
The value obtained by the algorithm has a good random distribution, so that the hash can obtain the best performance.
The algorithm should use at least one field.
The fields used in the algorithm should be constant.
Two string objects with the same text should return the same hash code value.
The gethashcode method implemented in system. Object returns a unique value within the appdomain range.
The gethashcode method of system. valuetype uses reflection to return the hash code value defined in the first instance field of the type.
We 'd better write the gethashcode method by ourselves, and the execution efficiency of our own writing is higher than that of our own.