Boxing and unboxing implementation in. NET-Practical tips

Source: Internet
Author: User

First look at the following code:

Copy Code code as follows:

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

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

This is because when an object is disassembled, the result of the transition must be its original unboxed type. You must first convert to an int type before you can convert to a double type. The correct format is as follows:
Copy Code code as follows:

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

in the. NET Framework, boxing (boxing) is typically composed 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 a value type instance to the memory of the newly allocated object on the managed heap.
3. Returns the address of the newly assigned object in the managed heap. Such an instance of a value type also becomes a reference type object.

and the unboxing (unboxing) process is as follows
1. If the object to be disassembled is null, a NullReferenceException exception will be thrown.
2. If the reference points to an object that is not a boxed object of the expected value type, the unboxing fails, and a InvalidCastException exception is thrown, as in the first part of this article.
3. A pointer to a part of the value type contained in the boxed object is returned. The value type that the pointer points to is ignorant of the additional members (that is, a method table pointer and a syncblockindex) that the reference type object usually has. In fact, the pointer points to the unboxed part of the Boxed object (Microsoft.NET Framework Program Design < revision >).

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

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.