C # basic object, new operator, type conversion,

Source: Internet
Author: User

C # basic object, new operator, type conversion,

Reference page:

Http://www.yuanjiaocheng.net/entity/delete-entity.html

Http://www.yuanjiaocheng.net/entity/add-entity-graph.html

Http://www.yuanjiaocheng.net/entity/update-entity-graph.html

Http://www.yuanjiaocheng.net/ASPNET-CORE/first.html

Http://www.yuanjiaocheng.net/ASPNET-CORE/asp-net-core-overview.html

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

Basic Method (public method of object ):

1. Equals: returns true if two objects have the same value.

2. GetHashCode: The returned object is worth a hash code. If an object of A type needs to be used as a key in a hash table set, this method should be rewritten for this type.

3. ToSting: returns a String object.

4. GetType: return an object instance derived from Typt, indicating the object type. The returned type object can be used with the reflection class to obtain metadata information related to the object type.

The following are protected methods:

5. memberwiseClon: Non-virtual method. You can create a new instance of the type and set the instance field of the new object to be exactly the same as the instance field of this object, A reference to the new instance is returned.

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

About the new operator Cao Yantao

Employee e = new Employee ("ConstructorParam1 ");

1. The number of bytes required for all the strength fields defined in the calculation type and all base types. Each object on the heap requires some additional members-"type object pointer)

"And" sync block index ", these members are used by ClR (common language runtime) to manage objects. The number of bytes of these additional members is the size of the incoming object.

2. It allocates bytes of the specified type from the managed heap to allocate the object memory. All allocated bytes are set to zero.

3. It initializes object type object pointers and synchronized index members.

4. The instance constructor of the call type, which is used to pass in any real parameters specified in the new call. (ConstructorParam1 indicates the real parameter)

After performing these operations, new returns a reference (or pointer) pointing to the new object ). The above reference will be saved to variable e, which has the Employee type.

 

The memory allocated to an object cannot be released. CLR adopts the garbage collection mechanism.

 

Type conversion

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

C # You can convert an object to any of its base classes at will. When you convert this type to another derived class, you need to perform display conversion. This conversion may fail at runtime.

Transformation using the is and as operators

Is checks whether an object is compatible with a specified type and returns true or false. The is operator does not throw an exception.

Object o = new Object ();

Boolean b1 = (o is Object) // return true

Boolean b2 = (o is Employee) // return false

If the object reference is null, false is always returned.

Is usually used in this way

If (o is Employee)

{

Employee e = (Employee) o;

// Use e in the remaining if statement

}

In this way, the CLR actually checks the Data Type twice.

As for its simplified writing

Employee e = o as Employee;

If (e! = Null)

{

// Use e for the remaining statement

}

In this way, the write CLR checks whether the o is compatible with the Employee ID. If yes, the non-null reference of the same object is returned.

 

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.