Same as reading CLR via C # (04) in Xiaojing.
When talking about classes in. net, they are born at the same root and cannot be too much. Because CLR requires that all classes inherit from system. object. All objects must provide a set of common operations, includingAdequacy,Uniqueness,Hash codeAndClone.
I,
Atomicity -- equals () method
Sometimes we need to compare whether two objects are equal, such as sorting and searching in an arraylist.
System. Object provides the equals () virtual method:
Class Object
{
Public Virtual Boolean equals (Object O)
{
If (this = O) return true;
Else return false;
}
}
This method is very simple: directly compare whether two references point to the same object. However, this comparison is inaccurate. Therefore, we need to rewrite this method to provide a more appropriate implementation method.
What are the four principles of equals () during rewriting?We seem to have learned this in discrete mathematics:
- Self-inverse. That is, X. Equals (x) must be true.
- Symmetric. That is, X. Equals (Y) and Y. Equals (x) must return the same value.
- It can be passed. That is, if both X. Equals (Y) and Y. Equals (z) return true, X. Equals (z) also returns true.
- Consistent. If the values of the two objects are not changed, the values for multiple comparisons should be the same.
Rewrite ideas
- 1. If the OBJ parameter is null, false is returned. In non-static methods, the current object indicated by this is certainly not null.
- 2. If the this and OBJ parameters point to the same instance object, true is returned. In this way, the field comparison process is omitted to improve the performance.
- 3. If this and OBJ parameters point to different object types, false is returned.
- 4. Compare each instance field in this and obj. If the field is not equal, false is returned.
- 5. Call the equals method of the base class. If the call result is false, false is returned;
- 6. So far, true can be returned.
II, Uniqueness -- referenceequals () method
Uniqueness indicates that two references point to the same object. Once our class overrides the equals method of ojbect, we cannot use it to detect uniqueness. Object provides another static method referenceequals ()
Public class object {
Public static Boolean referenceequals (Object obja, object objb ){
Return (obja = objb );
}
}
However, we try not to use the = Operator in C # To determine the uniqueness, unless both parameters are of the object type. It is possible that one of the parameter types will overwrite the = operator, which produces other semantics.
Instance experience?
Class animal {};
Static void main (string [] ARGs)
{
Animal a1 = new animal ();
Animal a2 = A1;
Console. writeline (object. referenceequals (a1, a2); // true, pointing to the same object
A2 = new animal ();
Console. writeline (object. referenceequals (a1, a2); // false, A2 points to the new object again
Int number = 100;
Console. writeline (object. referenceequals (number, number); // false, value type number is packed into different objects twice
Console. Read ();
}
III, Hash code -- gethashcode () method
System. Object provides the virtual method gethashcode () to get the int32 hash code from an object. This method returnsProgramThe unique value in the domain. The value does not change throughout the entire lifecycle of the object.
If we override the equals () method of the class, we 'd better rewrite the gethashcode () method, otherwise the compiler may generate warning information. Because the system. Collections. hashtable class requires that two identical objects have the same hash value.
Iv. Clone the icloneable Interface
If a class allows an instance to be copied, It inherits the icloneable interface.
Public interface icloneable {object clone ();}
Shortest copy?
When the field value of an object is copied, the object referenced by the field is not copied. You can call the memberwiseclone method of the object.
Deep copy?
Objects referenced by fields in the object instance are also copied. After the deep copy, the newly created object and the original object do not have any public things. Changing an object will not affect the other one.