(1) packing and unpacking

Source: Internet
Author: User

Packing: To convert a value type to a reference type, a mechanism called packing is required. Int32 A = 5; object o =;

During binning of the value type, the following internal events occur:

1. First, allocate the memory in the managed heap. The size is the amount of memory required for value-type fields plus the size of additional members (type object pointers and synchronized index blocks.

2. Copy the value type field to the newly allocated heap memory.

3. Return the object reference.

Unpack: First, unpacking is not the inverse process of packing. It is actually the process of getting a pointer, pointing to the original value type in an object.

After unpackingFollowed by the field copy process.

When disassembling an object, you can only convert it to a value type that is not originally boxed. Example: int32 x = 5; object o = x; int16 y = (int16) O;

In this case, an exception occurs. It can be written as follows: int16 y = (int16) (int32) O; must first be converted to the original type.

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.