C # Base Object, new operator, type conversion

Source: Internet
Author: User

Reference page:

Http://www.yuanjiaocheng.net/webapi/config-webapi.html

Http://www.yuanjiaocheng.net/webapi/web-api-route.html

Http://www.yuanjiaocheng.net/webapi/parameter-binding.html

Http://www.yuanjiaocheng.net/webapi/action-method-returntype.html

Http://www.yuanjiaocheng.net/webapi/web-api-reqresq-format.html

All types are eventually derived from the System.Object type.

The most basic method (public method of Object ):

1.Equals: Returns True if two objects have the same value.

2.GetHashCode: The return object is worth a hash code, and if an object of a type is to be used as key in a Hashtable collection, the type should override the method.

3.ToSting, returns a String object

4.GetType: Returns an instance of an object derived from Typt, indicating what type the object is. The returned type object can be used in conjunction with the reflection class to obtain metadata information about the type of the object.

The following are protected methods

5.MemberwiseClon: A non-virtual method that creates a new instance of a type and sets the instance field of the new object exactly as the instance field of the This object, returning a reference to the new instance.

6.Finalize: This virtual method is called during garbage collection.

About the new operator Cao Tao

Employee E=new Employee ("ConstructorParam1");

1. He calculates the number of bytes required for all strength fields defined in the type and all its base types. Each object on the heap needs some extra member--"type object pointer (pointer)

and synchronize index blocks (Sync block index), these members are used by the CLR (Common language Runtime) to manage objects. The number of bytes for these extra members is counted into the object size.

2. He allocates the memory of the object by assigning the required number of bytes from the managed heap, and all allocated bytes are set to zero.

3. He initializes the object's type object pointer and synchronizes the index member.

4. Invoke the instance constructor of the type and pass it to any arguments that are made in the call to new. (The upper ConstructorParam1 is the actual argument)

New will return a reference (or pointer) to the new object when it finishes performing these operations. The above reference is saved to the variable E, which has the employee type

Unable to display the release of memory allocated to an object, the CLR uses a garbage collection mechanism.

Type conversions

One of the most important features of the CLR is type safety.

C # can arbitrarily convert an object to any of his base classes, and when the type is converted to his derived class, a display conversion is required, and the conversion may fail at run time.

Use the IS and as operators to transform

Is checks whether an object is compatible with the specified type, returns True, or the false,is operator does not throw an exception.

Object O=new object ();

Boolean B1 = (O is Object)//returns True

Boolean B2 = (O is Employee)//returns False

Always returns False if the object reference is null

is typically used in this way

If (O is Employee)

{

Employee e= (employee) O;

In the If remainder statement, the E

}

So the CLR actually checked the data type two times

As for its simplified notation

Employee e= o as employee;

if (e!=null)

{

The remaining statements are in E

}

This writes the CLR to check if O is compatible with employee, and then returns a non-null reference to the same object.

C # Base Object, new operator, type conversion

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.