0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C#:拆箱装箱2

0
Last updated at Posted at 2017-02-17

.NET包含一个特殊的Object类,可以接受任意的数据类型的值,当所传递或所赋值的类型不是一个特定的数据类型时,object类就提供了一种传递参数和赋值的通用方法。赋给object的值必须作为引用类型,并存放砸托管堆中。

方法/步骤
1
装箱:
int age = 24;
object refAge= age;
可以看的出,第一条语句创建一个变量age,并将值放在托管栈中;
第二条语句将age的值赋给引用类型。它将值24放在托管堆中。
这个值类型包装为引用类型的过程,称为装箱。
alt
C#装箱和拆箱原理
2
拆箱:
相反,将引用类型转换为值类型的过程称为拆箱。拆箱将对对象强制转换为原来的类型。对前面的对象进行拆箱。
int newAge = (int) refAge;
string newAge =(String) refAge;
拆箱的值必须和它要转换的目标的变量有相同的类型。
C#装箱和拆箱原理

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?