Boxing and unboxing in C)

Source: Internet
Author: User

Packing and unpacking: any value type and reference type can be Object (Object) type conversion. Boxing refers to implicit or explicit conversion of a value type into Object Type, or convert this value type to an interface type applied by this value type ( Interface-type ). To pack a value of the value type, create Object Instance and copy the value to this Object , Boxed Object The data in the object is located in the heap, and the address in the heap is located 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 );//Explicit packing

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, useIsTo return a string, you can useObjectClassGetTypeMethod.

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, useIsTo return a string, you can useObjectClassGetTypeMethod.

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;

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.