".NET Framework Design" Chapter VI General Operation

xiaoxiao2021-03-06  84

First, the uniqueness of the object: Judging whether two references point to the same object? You can use the "= =" operator to compare two references, and you can use the static method of the ReferenceEquals of System.Object.

Second, the hash code of the object: Each object has an INT32 type hash code, so any object instance can be placed in a hash table collection.

Third, the object clone: ​​If a class wants that your instance can be cloned, the class should implement the Icloneable interface. The grammar is realized in the clone method of the interface.

Four, shallow copy: When the field value of the object is copied, the object referenced by the field will not be copied.

Deep copy: The object referenced in the object instance is also copied. For objects referenced in the objects referenced by the object instance field ... all copies. That is to say, after performing a deep copy, the original object and the newly created object will not share anything; changing an object does not have any effect on another object.

5, shallow copy and deep copy implementation: If you want the object's instance to implement a shallow copy, you can call System.Object's protected method MEMBERWISECLONE in the object's Clone method. Note: The MEMBERWISECLONE method does not call the constructor for the new object, and the value type instance itself supports a shallow copy. If you want to implement a deep copy, you need your own yourself in our reference type or value type of CLONE method (of course, you must implement the Icloneable interface). Therefore, an object's clone method may be a shallow copy, or it may be a deep copy, and the details of this object should be read.

Six, objects, etc.: The implementation of the equals method in the System.Oject type is compared whether the two references point to the same object. If we need to judge whether the two objects have the same "value", you must override the equals method. If the base type of our defined type does not inherit the implementation of the object.equals method (ie the base type also overwrites the equals method), then we should call the Equals method for the base type when rewriting the Equals method.

Seven, two forms of equals methods: There are two forms of equals.Object types:

The virtual function form public Virtual Bool Equals (Object); - it makes each object to override this method.

Static functional form public static bool equals (object, object);

- It guarantees that an exception is not thrown when an object parameter is null.

8. Different types of Equals methods for reference types and value types: The reference type inherited Equals judges that the reference is equal; the value of the value type inherited is the value equal.

Nine, the GetType method of the object is always the original type of object: for the sub-objects packaged by a parental outerwear, the GetType method is called, and the type returned is still a subclass type.

Ten, subclasses must also be compatible with the type of parent class: The IS operator returns TRUE when it is judged between an object and any of its base types.

XI, when the parent class function and the subclass function are renamed, the implementation of the constructor and the general function is different: when a subclass is initialized, automatically, according to the order of inheritance, from the parent class to subclasses one by one Class constructor. And if the parent class and subclasses have the same function (not constructor), the same name function of the parent class is not automatically called.

转载请注明原文地址:https://www.9cbs.com/read-120728.html

New Post(0)