C #---Boxing and unpacking

Source: Internet
Author: User
Tags mscorlib

Boxing and unpacking are the actions that are performed to convert between value types and reference types.

1. boxing occurs when a value type is converted to a reference type

2. unpacking occurs when a reference type is converted to a value type

The above two sentences are not difficult to understand, but deep understanding, it will take some space to explain.

Let's look at what happens when we start boxing, here's the simplest packing code in a row

?
objectobj = 1;

This line of statements assigns the integer constant 1 to the variable obj of type object, which is well known as a value type, where the value type is to be placed on the stack, and object is a reference type, which needs to be placed on the heap, and a boxing operation is required to place the value type on the heap.

The IL code for this line of statements is as follows, note that the comment section explains:

?
.locals init (   [0] objectobjValue //以上三行IL表示声明object类型的名称为objValue的局部变量  IL_0000: nop IL_0001: ldc.i4.s 9 //表示将整型数9放到栈顶 IL_0003: box [mscorlib]System.Int32 //执行IL box指令,在内存堆中申请System.Int32类型需要的堆空间 IL_0008: stloc.0 //弹出堆栈上的变量,将它存储到索引为0的局部变量中

This is what the boxing is going to do, and it is unavoidable to request a memory space on the heap and to copy the value type data on the stack to the requested heap memory space, which is sure to consume memory and CPU resources. Let's see what's going on with the unpacking operation:

Take a look at the following C # code:

?
objectobjValue = 4; intvalue = (int)objValue;

The above two lines of code perform a boxing operation to boxing the numeric constant 4 into a reference type, the object variable ObjValue, and then a unboxing operation that stores the reference variable ObjValue stored on the heap to the local value type variable value.

Again we need to look at the IL code:

?
.locals init (   [0] object objValue,   [1] int32 ‘value‘) //上面IL声明两个局部变量object类型的objValue和int32类型的value变量 IL_0000: nop IL_0001: ldc.i4.4 //将整型数字4压入栈 IL_0002: box [mscorlib]System.Int32  //执行IL box指令,在内存堆中申请System.Int32类型需要的堆空间 IL_0007: stloc.0 //弹出堆栈上的变量,将它存储到索引为0的局部变量中 IL_0008: ldloc.0//将索引为0的局部变量(即objValue变量)压入栈 IL_0009: unbox.any [mscorlib]System.Int32 //执行IL 拆箱指令unbox.any 将引用类型object转换成System.Int32类型 IL_000e: stloc.1 //将栈上的数据存储到索引为1的局部变量即value

The execution and boxing operations of a unboxing operation are reversed by converting the value of the reference type stored on the heap to a value type and to a value type variable.

Boxing and unpacking operations are additional CPU and memory resources, so generics are introduced after C # 2.0 to reduce the cost of boxing and unboxing operations.

C #---Boxing and unpacking

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.