4th Chapter Type Foundation--4.1 all types are derived from System.Object

Source: Internet
Author: User

4.1 All types are derived from System.Object

The runtime requires that each type eventually be derived from the System.Object type.

Since all types ultimately derive from System.Object , each object of each type guarantees a set of the most basic methods.

The System.Object class provides a common instance method as shown in the following table:

table 4-1 Public methods of System.Object
Equals Returns true if two objects have the same value. For details, refer to "Object Equality and Identity"
GetHashCode

Returns the hash code for the value of the object. If an object of a type is to be used as a key in a Hashtable collection (such as Dictionary ), the type should override the method.

Methods should provide a good distribution for different objects (that is, for all inputs, the hash value generated by GetHashCode should produce a random distribution in all integers.) ).

It is not appropriate to design this method into an Object . Most types will never be used as keys in a hash table: The method should be defined in the interface.

Tostring

The full name of the default return type (this. GetType (). FullName). However, this method is often overridden to return a String object that contains the state representation of an object. For example, a core type (such as Boolean and Int32 ) overrides the method to return a string representation of their value.

In addition, this method is often overridden for debugging purposes: A string is obtained after the call to display the values of the fields of the object. In fact, the debugger in Microsoft Visual Studio automatically calls the function to display the string representation of the object.

Note thatToString should theoretically perceive the CultureInfo associated with the calling thread and take action accordingly. character, string, and text processing will discuss the ToString in more detail.

GetType

Returns an instance of a type derived from type, indicating what type of object was called GetType .

The returned type object can mate with the reflection class to get metadata information about the type of the object. Reflection is discussed in "Assembly Loading and reflection".

GetType is a non-virtual method designed to prevent classes from overriding the method, concealing its type, and thus destroying type safety.

In addition, a type derived from System.Object can access the protected method as shown in table 4-2.

Table 4-2 protected methods for System.Object
MemberwiseClone

This non-virtual method creates a new instance of the type and sets the instance field of the new object exactly as the instance field of the This object.

Returns a reference to a new instance (the author refers to two different "instances" in this passage.) One is an instance of a class, that is, an object, and the other is an instance field defined in a class.

The so-called "instance field" refers to non-static fields, sometimes referred to as "instance members." Simply put, instance members belong to the class object, and static members belong to the class. ).

Finalize

After the garbage collector determines that the object should be reclaimed as garbage, the virtual method is called before the object's memory is actually reclaimed.

A type that needs to perform cleanup before reclaiming memory should override the method.

Managed heap and garbage collection discusses this important approach in more detail.

The CLR requires that all objects be created with the new operator. The following code shows how to create an Employee object:

Employee Employee = new Employee ("ConstructorParam1");

Here's what the new operator does:

    1. Computes the number of bytes required for all instance fields defined in the type and all of its base types (until System.Object, although it does not define its own instance field). Each object on the heap requires some extra members (called overhead members, or "cost members"), including "type Object pointers" (types pointer) and "Sync Block index". The CLR uses these members to manage objects. The number of bytes in the extra member is counted to the object size.
    2. Allocates the memory of the object from the managed heap by allocating the number of bytes required by the core, and all allocated bytes are set to 0 (0).
    3. Initializes the object's "type Object pointer" and "synchronized Block index" members.
    4. Invokes the instance constructor of the type, passing the argument specified in the new call (the previous example is the string "ConstructorParam1"). Most compilers are automatically generated in the constructor to call the base class constructor. Each type of constructor is responsible for initializing the instance fields of the type definition. Eventually called the System.Object Constructor, the constructor does nothing and simply returns.

new Returns a reference (or pointer) to the new object after all of these operations have been performed. In the preceding example code, the reference is saved to the variable employee, which has the employee type.

BTW, there is no delete operator corresponding to the new operator; in other words, there is no way to explicitly release the memory that has been allocated for the object. The CLR uses a garbage collection mechanism to automatically detect that an object is no longer being used or accessed and automatically frees the object's memory.

4th Chapter Type Foundation--4.1 all types are derived from System.Object

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.