System. Object Class

Source: Internet
Author: User

All classes in C # are directly or indirectly inherited from the system. Object Class, which enables the class in C # to be inherited by a single root. If the inheritance class is not explicitly specified, the compiler determines that the class inherits from the system. Object Class by default. The system. object class can also be expressed by the lower-case object keyword. The two are exactly the same. Naturally, all classes in C # inherit the public interfaces of the system. object class, and it is very important for us to understand and master the behavior of classes in C. The following is a system. object class that is expressed in the form of an interface only:

namespace System{public class Object{public static bool Equals(object objA,object objB){}public static bool ReferenceEquals(object objA,object objB){}public Object(){}public virtual bool Equals(object obj){}public virtual int GetHashCode(){}public Type GetType(){}public virtual string ToString(){}protected virtual void Finalize(){}protected object MemberwiseClone(){}}

Let's first look at the two static methods of the object: equals (Object obja, object objb), referenceequals (Object obja, object objb), and an instance method equals (Object OBJ ). Before describing these two methods, we must first understand the two important equality concepts of object-oriented programming: equal values and equal references. Equal values mean that their data members are equal by memory bit. When the references are equal, they point to the same memory address, or their object handles are equal. If the reference value is equal, the reference value must be equal. For the value type relationship equal sign "=", determine whether the two values are equal (the structure type and enumeration type have no defined relationship equal sign "=", we must define it ourselves ). For the reference type relationship equal sign "=", determine whether the two references are equal. The Value Type in C # usually does not reference equal representation. It is used to indirectly judge whether the addresses of the two are equal only by using the "&" in the unmanaged programming.

The static method equals (Object obja, object objb) first checks whether both objects obja and objb are null. If yes, true is returned; otherwise, obja is performed. equals (objb) calls and returns its value. The problem comes down to the instance method equals (Object OBJ ). The default implementation of this method is {return this = OBJ;}, that is, to determine whether two objects are referenced to be equal. However, we noticed that this method is a virtual method. C # we recommend that you rewrite this method to determine whether the values of the two objects are equal. Microsoft. the method is overwritten by many types provided in the. NET Framework class library, such as: system. string (string), system. int32 (INT), but some types do not overwrite this method, such as: system. array, we must pay attention to it when using it. For the reference type, if the instance method equals (Object OBJ) is not overwritten, the call to it is equivalent to this = OBJ, that is, the reference equals judgment. All value types (implicitly inherited from the system. valuetype class) overwrite the instance method equals (Object OBJ) to determine whether the values are equal.

Note that false is returned for object X, X. Equals (null). Here, X cannot be null (otherwise, equals () cannot be called, and the system throws a null reference error ). Here we can also see the reason for designing the static method equals (Object obja, object objb)-If both objects obja and objb can be null, we can only use object. equals (Object obja, object objb) is used to determine whether the values are equal. Of course, if we do not rewrite the instance method equals (Object OBJ), we still get the result of referencing equal values. We can implement the interface icomparable (We will elaborate on the interface inheritance and Polymorphism in section 7) to force rewrite the instance method equals (Object OBJ ).

For the value type, the instance method equals (Object OBJ) should be consistent with the return value of the equal sign "=", that is, if we overwrite the instance method equals (Object OBJ ), we should also overload or define the equal sign "=" operator, and vice versa. Although the value type (inherited from system. valuetype class) All override the instance method equals (Object OBJ), but C # we recommend that you override your own value type instance method equals (Object OBJ), because the system. valuetype class rewriting is inefficient. For the reference type, we should override the instance method equals (Object OBJ) to express equal values. Generally, we should not overload the equal sign "=" operator because its default syntax is to judge that the reference is equal.

The static referenceequals (Object obja, object objb) method is used to determine whether two objects are referenced to be equal. If the two objects are of reference type, their semantics is the same as that of the equal sign "=" operator without overloading. If two objects are of the value type, the return value must be false.

The instance method gethashcode () provides the hash value for the corresponding type and applies it to the hash algorithm or hash table. It should be noted that if we overwrite the equals (Object OBJ) method of an instance type, we should also overwrite the instance method gethashcode () -- this is the case, the values of the two objects are equal, their hash codes should also be equal. The following code is a good example of the previous methods:

Using system; struct a {public int count;} Class B {public int number;} class c {public int integer = 0; Public override bool equals (Object OBJ) {c = OBJ as C; If (C! = NULL) return this. integer = C. integer; elsereturn false;} public override int gethashcode () {return 2 ^ integer;} class test {public static void main () {A A1, A2; a1.count = 10; a2 = A1; // console. write (a1 = a2); the "=" operator console is not defined. write (a1.equals (A2); // trueconsole. writeline (object. referenceequals (a1, a2); // falseb b1 = new B (); B b2 = new B (); b1.number = 10; b2.number = 10; console. write (b1 = b2); // falseconsole. write (b1.equals (B2); // falseconsole. writeline (object. referenceequals (b1, b2); // falseb2 = b1; console. write (b1 = b2); // trueconsole. write (b1.equals (B2); // trueconsole. writeline (object. referenceequals (b1, b2); // truec C1 = new C (); C C2 = new C (); c1.integer = 10; c2.integer = 10; console. write (C1 = c2); // falseconsole. write (c1.equals (C2); // trueconsole. writeline (object. referenceequals (C1, C2); // falsec2 = C1; console. write (C1 = c2); // trueconsole. write (c1.equals (C2); // trueconsole. writeline (object. referenceequals (C1, C2); // true }}

As we expected, compile and run the program and we will get the following output:

    Truefalse
    Falsefalsefalse
    Truetruetrue
    Falsetruefalse
    Truetruetrue

The instance method GetType () and typeof have the same semantics. They all query the object metadata to determine the object runtime type, we will elaborate on the feature and ing in the tenth lecture.

The instance method tostring () returns the string expression of the object. If this method is not overwritten, the system generally returns the type name as a string.

The protected finalize () method has special semantics in C #, which will be described in detail in "lecture 5 constructor and destructor.

The protected memberwiseclone () method returns a "Shadow Copy" of the current object. This method cannot be overwritten by the quilt class. "Shadow Copy" is only a bitwise copy of an object. It means to assign a value to the value type variable in the object and copy the reference type variable in it, that is, the copied reference variable will hold a reference to the same memory. Compared with "Shadow Copy", it performs value replication instead of handle replication on referenced variables. For example, X is an object that contains objects a and B, and object A contains references of object m. Y is a "Shadow Copy" of X ". Then y will have the same reference of A and B. However, for a "Deep copy" Z of X, it will have references to objects C and D, and an indirect reference to object n, where C is a copy of, D is a copy of B, and N is a copy of M. Deep copy is completed in C # by implementing the icloneable interface (providing the clone () method.

Object and system. the grasp of objects makes a good foundation for class learning, but this is only a small step in our sharp journey. It is related to object member initialization, memory reference release, inheritance and polymorphism, exception Handling and many other "sharp" special effects can be achieved, so we will continue to look forward to the following topics!

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.