C # Learning diary 18 ---- boxing conversion and unboxing conversion

Source: Internet
Author: User

C # Learning diary 18 ---- boxing conversion and unboxing conversion

After learning above, we can basically convert the value type to the value type. The conversion between the reference type and the reference type, but the value type and the reference type (object) how can we implement the conversion between them ??

In C #, we use packing to convert the value type to the reference type, and use unpacking to convert the reference type to the value type. In other words, it can be converted from any value type, reference type to Object type. With packing and unpacking, we can think of it as an object type for any type. (You can clickReference Type ---- object classEnter)

Packing conversion:

Here I want to add, in. in. NET, the data type is divided into value type and reference (not the same as the pointer of C ++) type. Correspondingly, the memory allocation is divided into two methods: Stack, 2. Heap (hosting heap .) The value type is only allocated in the stack. Allocate memory and managed heap for reference types.

Write an example:

Int I = 123;

Object o = I; // It is also an implicit conversion to implement the packing operation.

 

The principle of packing conversion is as follows:

 

Packing conversion is essentially a copy type conversion. That is to say, after we finish packing, changing the I value o will not change.

 

Binning conversion:

In contrast to the packing conversion, the unpacking conversion refers to converting an object type to a value type. The unpacking process is divided into two steps. First, check this object instance, check whether it is a boxed value of the given value type, and then copy the value of this instance to the variable of the value type;

Write an example (based on the preceding Packing code ):

Int n_int = (int) o; // forcibly convert the object type to the int type

 

It can be seen that it is the inverse process of packing, and the object is forcibly converted to the original type, it must be noted that, the binning value must be of the same type as the variable to be converted.

 

 

Here, it is necessary to emphasize that packing and unpacking are conversions from the value type or reference type to the object type, from the object type to the value type or reference type.

 

 

 

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.