What is System.Object? How to use System.Object?

Source: Internet
Author: User
Public method in Object Explanation: Public method: Equals:public class Object {publicly virtual Boolean Equals (object obj) {///If two references point to the same object, They must contain the same value if (this = = obj) return true;return false;}} It returns true if the This and obj instances refer to the same object. It seems reasonable because equals knows that the object must contain the same value as himself. However, if the instance refers to a different object, equals will not determine if the object contains the same value, so it returns false. In other words, for the default implementation of the Equals method of object, he is implementing the actual identity rather than equality. The correct implementation of the Equals method: 1, returns False if the obj argument is null, because when a non-static Equals method is called, the current object identified by this is obviously not null2 and returns true if the this and the obj arguments refer to the same object. This step helps to improve performance when comparing objects that contain a large number of fields, and returns False if the this and the obj arguments refer to objects of different types. A string object is obviously not equal to a FileStream object 4, for each field defined by the type, compares the value in the this object to the value in the Obj object, any field is not equal, returns FALSE5, calls the Equals method of the base class to compare any fields of his definition Returns False if the Equals method method of the base class returns false, otherwise it returns true note: The test for the same problem uses the static method of object ReferenceEquals, which is prototyped as follows: public static Boolean ReferenceEquals (Object Obja, Object OBJB) {return (OBJA==OBJB);} Check for identity issues (see if two references point to the same object) be sure to call ReferenceEquals, and you should not use the c # = = operator (unless you first transform two operands to object), because the type of an operand may overload the = = character, assigning it a value different from the semantics of the same. System. ValueType (the base class for all value types) overrides the Equals method of object, and the correct implementation is performed to perform the equality check of the value (rather than the identity check), and the internal implementation of ValueType is as follows: 1, if the obj argument is null, Returns FALSE2, if this and obj are realThe reference to different types of objects returns FALSE3, and the values in the This object are compared to the values in the Obj object (not the identity check) for each instance field defined by the type. The implementation of ValueType equals is as follows: 1, if the obj argument is null, returns FALSE2, if the this and the obj arguments refer to objects of different types, returns FALSE3, for each instance field defined by the type, All values in the This object are compared to the values in the Obj object (by calling the Equals method of the field), and any fields are not equal, returning FALSE4, which returns True. The Equals method of ValueType does not invoke the Equals method of object internally, the ValueType Equals method uses reflection to complete step 3 above, because the CLR reflection mechanism is slow, and the Equals method should be overridden to provide its own implementation when defining its own value type. thereby improving the performance of their type of strength for equality comparisons, of course, their implementation does not call base. equals. Note: When you define your own type, the overridden Equals 4 features that conform to equality 1,equalse must be reflexive: x.equals (x) must return true2,equals to be symmetric: x.equals (y) and Y. Equals (x) returns the same value 3,equals must be transitive: x.equals (y) returns True,y.equals (Z) returns True, X. Equals (z) definitely returns true4,equals must be consistent. Compare two values unchanged, equals return value does not change GetHashCode: When you define a class that overrides the Equals method, you must also override the GetHashCode method. The reason for this requirement is due to the System.Collections.Hashtable type, The implementation of the System.Collection.Generic.Dictionary type and some other collections requires that both objects must have the same hash code to be considered equal. So overriding equals must override GetHashCode to ensure equality algorithm and object hash code algorithm consistency. Simply put, adding a key/value pair to the collection is the first thing to get the hash code for the object. The hash code indicates that the key/value pair is to be stored in that hash bucket. When the collection requires a lookup key, it gets the hash code for the specified key object that identifies the hash bucket that is now being searched sequentially, and in which the key object that is equal to the specified key object is found. Using this algorithm to store and find keys means that once a key object is modified for the collection, the collection can no longer find the object. Therefore, when you need to modify the key object in the hash table, the correct way is to remove the original key/value pairs, modifyKey object, and then add the new key/value pair back to the hash table. Customizing the GetHashCode method may not be a difficult task, but it depends on the data type and the distribution of the data. An example and a rule are given below: internal sealed class Point{private ReadOnly Int32 m_x, m_y;public override int GetHashCode () {return m_x ^ m_ Y Returns the XOR result of M_x and M_y}} rules: 1, this algorithm to provide a good random distribution, so that the hash table to obtain the best performance 2, the algorithm can call the accumulated GetHashCode method, and include his return value. But generally do not call the GetHashCode method of object or ValueType, because both implementations are not close to the high-performance hashing algorithm 3, the algorithm executes as fast as 4, different objects containing 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, and the GetHashCode method implemented by System.Object does not know anything about derived types and other fields, so returns a number that is guaranteed to be constant for the lifetime of the object. ToString: The full name of the default return type (this. GetType (). FullName) GetType: Returns an instance of a type derived from type, indicating what type to call the object of GetType. The returned type object can mate with the reflection class to get metadata information about the type of the object. In addition, GetType is a non-virtual method to prevent classes from rewriting the FANGFA, concealing the type of the enterprise, and thus destroying the type safety. Protected method: MemberwiseClone: This non-virtual method creates a new instance of the class, and the instance field of the static object is exactly the same as the instance field of the This object. Returns a reference to the new instance. Finalize: After the garbage collector determines that the object is reclaimed as garbage, the virtual method is called before the object's memory is actually reclaimed. The type of cleanup work that needs to be performed before recycling should override the method.
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.