The boxing and unboxing implementation process in. NET

Source: Internet
Author: User
Let's look at the following code:

int tempi = 1; Object o = tempi; Double tempd = (double) o;

Compile-time can pass, but the runtime reported the following error:
System.InvalidCastException: The specified conversion is not valid.

This is because, when unpacking an object, the result of the transformation must be the type it was originally unboxed. This must be converted to an int type before it can be converted to a double type. The correct format is as follows:

int tempi = 32; Object o = tempi; Double tempd = (double) (int) O;

In the. NET Framework, boxing (boxing) typically consists of the following three steps:
1. Allocates memory from the managed heap for the newly generated reference type object. The allocated memory size is the size of the boxed value type instance itself, plus a method table pointer and a syncblockindex added for the newly generated reference type.
2. Copy the field of the value type instance to the memory of the newly allocated object on the managed heap.
3. Returns the address of the newly allocated object in the managed heap. Such a value type instance becomes a reference type object as well. The

and unpacking (unboxing) procedure is as follows:
1. If the object to be disassembled is null, a NullReferenceException exception will be thrown.
2. If the reference to the object is not a boxed object of the expected value type, the unboxing fails and throws a InvalidCastException exception (as in the beginning of this article).
3. A pointer to a part of the value type contained in an already boxed object is returned. The value type that the pointer points to is unknown to the additional member (that is, a method table pointer and a syncblockindex) that the reference type object typically has. In fact, the pointer points to the unboxed part of the already boxed object (Microsoft.NET Framework Programming < revision >).

for the 3rd, you can use the example above to help understand. First define the value type variable tempi, which occupies 4 bytes in memory, and after boxing, it becomes a reference object while adding a method table pointer and a syncblockindex. For reference types, you can get their values, method table pointers, and syncblockindex by simply passing in the address of a "reference type". When unpacking, the address of its value is passed (the unboxed part), which is the address (reference) of an "int (Int32) type", which allows only 4 bytes to be read. The double type is 8 bytes, so the implicit conversion is an error, and it needs to be converted to type int before it can be converted to a double 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.