Home > Backend Development > C#.Net Tutorial > The implementation process of boxing and unboxing in .NET

The implementation process of boxing and unboxing in .NET

高洛峰
Release: 2017-01-24 14:17:33
Original
1225 people have browsed it

Look at the following code first:

int tempi = 1; 
object o = tempi; 
double tempd = (double) o;
Copy after login

It can pass when compiling, but the following error is reported when running:
System.InvalidCastException: The specified conversion is invalid.

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

int tempi = 32; 
object o = tempi; 
double tempd = (double)(int) o;
Copy after login

In the .NET framework, boxing usually consists of the following three steps:
1. Allocate memory for the newly generated reference type object from the managed heap. 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 fields of the value type instance to the memory of the newly allocated object on the managed heap.
3. Return the address of the newly allocated object in the managed heap. In this way, the value type instance also becomes a reference type object.

The unboxing process is as follows:
1. If the object to be unboxed is null, a NullReferenceException will be thrown.
2. If the object pointed to by the reference is not a boxed object of the expected value type, the unboxing fails and an InvalidCastException is thrown (as at the beginning of this article).
3. A pointer to the value type part contained in the boxed object is returned. The value type pointed to by this pointer knows nothing about the additional members that reference type objects usually have (ie, a method table pointer and a SyncBlockIndex). In fact, the pointer points to the unboxed portion of the already boxed object (Microsoft.NET Framework Programming ).

For point 3, you can use the above example to help understand. First define the value type variable tempi, which occupies 4 bytes in memory. After boxing, it becomes a reference object and adds a method table pointer and a SyncBlockIndex. For reference types, you only need to pass the address of a "reference type" to get its value, method table pointer and SyncBlockIndex. When unboxing, what is passed is the address of its "value" (the unboxed part), that is, an address (reference) of type "int (Int32)", which only allows reading 4 bytes. The double type is 8 bytes, so the implicit conversion will report an error. It needs to be converted to the int type before it can be converted to the double type.

For more articles related to the implementation process of boxing and unboxing in .NET, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template