Boxing and unboxing in C)

Source: Internet
Author: User

Packing and unpacking: any value type and reference type can be converted to the object type. Boxing conversion refers to implicit or explicit conversion of a value type into an object type, or convert the value type to an interface-type applied to the value type ). To pack a value of the value type, create an object instance and copy the value to the object. The data in the packed object is in the heap, and the address in the heap is in the stack. The value of the boxed type is assigned to the object as a copy. For example:
Int I = 10;
Object obj = I; // implicit packing
Object obj = object (I); // explicitly boxed
If (obj is int) // int
Console. WriteLine ("OK ");
Console. WriteLine (obj. GetType (); // System. Int32
There are two ways to view the type of raw data packaged in the referenced object after packaging. To determine whether the original type is a given atomic type, use is. To return a string, use the GetType method of the object class.

Box-breaking conversion refers to explicitly converting an object type into a value type, or explicitly converting an interface type into a value type that executes this interface. Note that the packing operation can be performed implicitly, but the unpacking operation must be explicit. The unpacking process is divided into two steps: first, check the object instance to see if it is a boxed value of the given value type. Then, copy the Instance value to the value type variable. For example:
Int I = 10;
Object obj = I;
Int j = (int) obj;
There are two ways to view the type of raw data packaged in the referenced object after packaging. To determine whether the original type is a given atomic type, use is. To return a string, use the GetType method of the object class.

Box-breaking conversion refers to explicitly converting an object type into a value type, or explicitly converting an interface type into a value type that executes this interface. Note that the packing operation can be performed implicitly, but the unpacking operation must be explicit. The unpacking process is divided into two steps: first, check the object instance to see if it is a boxed value of the given value type. Then, copy the Instance value to the value type variable. For example:
Int I = 10;
Object obj = I;
Int j = (int) obj;

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.