System. Object introduction, system. object

Source: Internet
Author: User

System. Object introduction, system. object
Explanation of public methods in Object: public method: Equals: public class Object {public virtual Boolean Equals (Object obj) {// If two references point to the same Object, they must include the same value if (this = obj) return true; return false ;}}. if this and obj instances reference the same object, true is returned. It seems reasonable because Equals knows that an object must contain the same value as itself. However, if the instance references different objects, Equals returns false because it is not sure whether the object contains the same value. In other words, for the default implementation of the Equals method of an Object, the actual implementation is identity, rather than equality. The correct implementation of the Equals method: 1. If the obj real parameter is null, false is returned, because when the non-static Equals method is called, the current object identified by this cannot be null2, if this and obj parameters reference the same object, true is returned. When comparing objects with a large number of fields, this step helps improve performance 3. If this and obj parameters reference different types of objects, false is returned. A string object is obviously not equal to a FileStream object 4. For each field of the type definition, compare the value of this object with the value of obj object. If any field is not equal, false5 is returned, call the Equals method of the base class to compare any fields defined by the base class. If the Equals method of the base class returns false, false is returned. Otherwise, true is returned. Note: the Object static method ReferenceEquals is used to test the identity problem. Its prototype is as follows: public static Boolean ReferenceEquals (Object objA, object objB) {return (objA = objB );} check the identity problem (to see if two references point to the same Object). Be sure to call ReferenceEquals. Do not use the = Operator of C # (unless you first convert both operands into objects ), because the type of an operand may overload the = character, assigning a value to it is different from the same semantics. System. ValueType (base classes of all value types) overrides the Equals method of the Object, and performs a correct implementation to perform a value equality check (instead of a identity check ), the Equals of ValueType is implemented as follows: 1. If the obj real parameter is null, false2 is returned. If this and obj real parameters reference different types of objects, false3 is returned, for each instance field defined by the type, the value of this object is compared with the value of obj object (instead of the same identity check ). The implementation of Equals in ValueType is as follows: 1. If the obj real parameter is null, false2 is returned. If this and obj real parameters reference different types of objects, false3 is returned, for each instance field defined by the type, the value in this object is compared with the value in the obj object (by calling the equals method of the Field). If any field is not equal, false4 is returned, returns true. The ValueType Equals method does not call the Object's Equals method internally. The ValueType Equals method completes step 3 by Using Reflection. Due to the slow CLR reflection mechanism, when defining your own value types, you should override the Equals method to provide your own implementation, so as to improve the performance of your type for Equality comparison. Of course, your own implementation does not call the base. equals. note: When defining your own type, the overwritten Equals must conform to the equality of the four feature 1, and the reverse se must be self-reversed: x. equals (x) certainly returns true2, Equals must be symmetric: x. equals (y) and y. equals (x) returns the same value 3. Equals must be passed: x. equals (y) returns true, y. if Equals (z) returns true, x. equals (z) certainly returns true4, Equals must be consistent. Compare two values without changing, and the Equals return value does not change GetHashCode: when your defined class overwrites the Equals method, you must also override the GetHashCode method. The reason for this requirement is that the System. collections. hashtable type, System. collection. generic. in the implementation of the Dictionary type and some other sets, the two objects must have the same hash code to be considered equal. Therefore, to rewrite Equals, you must overwrite GetHashCode to ensure consistency between the equality algorithm and the object hash code algorithm. Simply put, to add a key/value pair to a set, you must first obtain the object's hash code. The hash code indicates the key/value pair to be stored in that hash bucket. When the set needs to find the key, it will get the hash code of the specified key object, which identifies the hash bucket to be searched in order, searches for key objects that are equal to the specified key object. This algorithm is used to store and search keys, which means that once a key object of the set is modified, the set will no longer be able to find the object. Therefore, when you need to modify the key object in the hash table, the correct method is to remove the original key/value pair, modify the key object, and then add the new key/value pair back to the hash table. Customizing the GetHashCode method may not be difficult, but it depends on the Data Type and distribution. Here is an example and rule: internal sealed class Point {private readonly Int32 m_x, m_y; public override int GetHashCode () {return m_x ^ m_y; // return the XOR results of m_x and m_y} rule: 1. This algorithm must provide a good random distribution so that the hash table has the best performance. 2, you can call the accumulated GetHashCode method in an algorithm and include its return value. However, do not call the GetHashCode method of the Object or ValueType, because the implementation of both methods is independent of the high-performance hashing algorithm, and the algorithm execution speed is as fast as possible, different objects with the same value should return the same hash value. For example, two string objects that contain the same text return the same hash code 5, System. the GetHashCode method implemented by the Object does not know anything about the derived type and other fields. Therefore, a number is returned for the lifetime of the Object. ToString: The full name of the returned Type (this. GetType (). FullName) GetType: returns an instance of the Type derived from Type, indicating the Type of the object that calls GetType. The returned Type object can work with the reflection class to obtain metadata information related to the object Type. In addition, GetType is a non-virtual method, which aims to prevent the class from overwriting the fangfa, conceal the enterprise type, and damage the type security. Protected method: MemberwiseClone: this non-virtual method creates a new instance of the class, and the instance field of the static object is set to be exactly the same as the instance field of this object. Returns the reference to the new instance. Finalize: this virtual method is called after the Garbage Collector determines that the object is recycled as garbage and before the object's memory is actually recycled. This method should be rewritten for the type of Cleanup before recycling.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.