Home Backend Development C#.Net Tutorial Detailed explanation of boxing and unboxing in C#

Detailed explanation of boxing and unboxing in C#

Jan 24, 2017 pm 02:20 PM

Boxing and unboxing are operations to be performed to convert between value types and reference types.
1. Boxing occurs when a value type is converted to a reference type.
2. Unboxing occurs when a reference type is converted to a value type.
It is not difficult to understand the above two sentences, but if you go deeper, It takes some space to explain.
Let’s first look at what happens during boxing. The following is the simplest line of boxing code

object obj = 1;
Copy after login

This line of statement assigns the integer constant 1 to the object type variable obj; as we all know, the constant 1 is Value types, value types need to be placed on the stack, and object is a reference type, which needs to be placed on the heap; to put the value type on the heap, a boxing operation needs to be performed.
The IL code of this line of statement is as follows. Please pay attention to the comment part:

.locals init (
  [0] object objValue
)  //以上三行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的局部变量中
Copy after login

The above is the operation to be performed for boxing. When performing boxing operation, it is inevitable to apply for memory space on the heap. And copy the value type data on the stack to the applied heap memory space, which will definitely consume memory and CPU resources. Let’s take a look at how the unboxing operation works:
Please look at the following C# code:

object objValue = 4;
int value = (int)objValue;
Copy after login

The above two lines of code will perform a boxing operation to box the integer constant 4 into a reference type object variable objValue; and then perform an unboxing operation to store the reference variable objValue stored on the heap into the local integer value type variable value.
We also need to look at the IL code:

The execution process of the
.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
Copy after login
unboxing operation is exactly the opposite of the boxing operation process. It converts the reference type value stored on the heap into a value type and gives it to the value type variable.

Boxing operations and unboxing operations consume additional CPU and memory resources, so generics were introduced after c# 2.0 to reduce the consumption of boxing operations and unboxing operations.

For more detailed explanations of boxing and unboxing in C#, please pay attention to the PHP Chinese website!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to add next-level C compiler How to add next-level C compiler Mar 03, 2025 pm 05:44 PM

How to add next-level C compiler

What are the alternatives to NULL in C language What are the alternatives to NULL in C language Mar 03, 2025 pm 05:37 PM

What are the alternatives to NULL in C language

Method of copying code by C language compiler Method of copying code by C language compiler Mar 03, 2025 pm 05:43 PM

Method of copying code by C language compiler

Which C language compiler is better? Which C language compiler is better? Mar 03, 2025 pm 05:39 PM

Which C language compiler is better?

What are the web versions of C language compilers? What are the web versions of C language compilers? Mar 03, 2025 pm 05:42 PM

What are the web versions of C language compilers?

Is NULL still important in modern programming in C language? Is NULL still important in modern programming in C language? Mar 03, 2025 pm 05:35 PM

Is NULL still important in modern programming in C language?

C language online programming website C language compiler official website summary C language online programming website C language compiler official website summary Mar 03, 2025 pm 05:41 PM

C language online programming website C language compiler official website summary

C language compiler installation tutorial (computer version) C language compiler installation tutorial (computer version) Mar 03, 2025 pm 05:41 PM

C language compiler installation tutorial (computer version)

See all articles