[DotNet] Deep understanding of C # Boxing and unpacking

Source: Internet
Author: User
Tags dotnet 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

Object obj = 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] object ObjValue)  //above three line il means a local variable that declares the name of the object type ObjValue IL_0000:NOPIL_0001:LDC.I4.S 9// Indicates that the integer number 9 is placed on top of the stack Il_0003:box [mscorlib]system.int32//Execute IL box instruction, in the memory heap to request the System.Int32 type required heap space il_0008:stloc.0//popup stack on the variable, Store it in a local variable indexed to 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:

Object ObjValue = 4;int value = (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 ')//above IL declaration Two local variables the value variable of the ObjValue and int32 types of type Object il_0000 : nopil_0001:ldc.i4.4//Pressing the integer number 4 into the stack Il_0002:box [mscorlib]system.int32  //execute IL box instruction, requesting the heap space Il required for the System.Int32 type in the memory heap _0007:stloc.0//POPs a variable on the stack, stores it in a local variable indexed to 0 il_0008:ldloc.0//the local variable indexed to 0 (that is, the ObjValue variable) into the stack IL_0009:unbox.any [mscorlib] System.Int32//Execute IL unboxing Directive unbox.any converts a reference type object to System.Int32 type IL_000E:STLOC.1//stores data on the stack to a local variable indexed to 1 that is 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.

[DotNet] Deep understanding of 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.