【发布时间】:2012-02-07 06:13:19
【问题描述】:
我阅读了What is boxing and unboxing and what are the trade offs?,但无法理解一件事。假设我有一堂课:
class MyClass
{
public int Value { get; set; }
}
我想在我的方法中获得价值:
void MyFunc(MyClass cls)
{
int i = cls.Value;
}
作为一个放置在堆中的类,我猜 Value 也放置在堆中?因此操作
int i = cls.Value;
正在拆箱吗?还是不拆箱?
【问题讨论】:
-
不,这不是拆箱,因为您要从一个 int 字段转到一个 int 局部变量。如果一侧是引用类型,则将是装箱/拆箱,因此如果 Value 是对象类型。