当引用类的值类型成员时是否会发生拆箱?
我读了 什么是装箱和拆箱?权衡是什么? 但无法理解一件事。假设我有一个类:
class MyClass
{
public int Value { get; set; }
}
并且我想在我的方法中获取值:
void MyFunc(MyClass cls)
{
int i = cls.Value;
}
作为放置在堆中的类,我想 Value 也放置在堆中吗?那么操作
int i = cls.Value;
就是拆箱吗?还是说没开箱?
I read What is boxing and unboxing and what are the trade offs? but can't understand one thing. Suppose I have a class:
class MyClass
{
public int Value { get; set; }
}
And I want to get value within my method:
void MyFunc(MyClass cls)
{
int i = cls.Value;
}
As a class placed in heap, I guess that Value placed in a heap too? And therefore operation
int i = cls.Value;
is unboxing? Or it's not unboxing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
停止考虑堆栈和堆;这是完全错误的思考方式。显然,“装箱”的意思不是“在堆上”,因此任何“在堆上”的东西都必须是“装箱”的。
栈和堆是无关的。相反,请考虑引用和值。 当值类型的值必须被视为对对象的引用时,它会被装箱。如果您需要对值类型的值的引用,则创建一个框,将值放入框,并参考该框。现在您有了对值类型的值的引用。
不要将其与引用值类型的变量相混淆;那是完全不同的。变量和值是两个截然不同的东西;要引用变量,您可以使用“ref”关键字。
Stop thinking about stack and heap; that's completely the wrong way to think about it. It is emphatically not the case that "boxed" means "on the heap", and therefore anything "on the heap" must be "boxed".
Stack and heap are irrelevant. Rather, think about references and values. A value of value type is boxed when it must be treated as a reference to an object. If you need to have a reference to a value of a value type, you make a box, put the value in the box, and make a reference to the box. And there, now you have a reference to a value of value type.
Do not confuse that with making a reference to a variable of value type; that is completely different. A variable and a value are two very different things; to make a reference to a variable you use the "ref" keyword.
装箱或拆箱与在堆或堆栈上存储值没有任何关系。您应该阅读C# 编程指南中的文章“装箱和拆箱” 。在您的示例中,这两种情况都没有发生,因为您将 int 分配给 int。
Boxing or unboxing doesn't have anything to do with storing values on heap or stack. You should read the article "Boxing and Unboxing" from the C# Programming Guide. In your example none of these two occurs because you're assigning int to int.
它既不是
拆箱
也不是装箱
。考虑到您分配给
int
不带强制转换,并且我希望这段代码能够编译,这意味着cls.Value
是一个Integer( int)
类型。因此将int
分配给int
。这里发生的是一个值副本。
It's neither
unboxing
norboxing
.Considering you assign to
int
without cast and, I hope, this code compiles, that means thatcls.Value
is aInteger(int)
type. So assignint
toint
.What happens here is a value copy.
请注意,我们不会将
i
分配给对象的字段或属性,而是分配给对象本身。它可以与光的性质相媲美。光可以被认为是由粒子(光子)组成的,或者是一种波。
int
可以是int
对象(引用类型)或int
值类型。但是,您不能直接将int
定义为引用类型;您必须将其转换为对象,例如将其分配给object
类型的变量、参数或属性,或将其转换为object
以使其成为引用类型。Note that we do not assign
i
to a field or property of an object, but to the object itself.It is comparable to the nature of light. Light can be perceived of being made of particles (photons) or being a wave. An
int
can be anint
object (a reference type) or anint
value type. You can however not define anint
to be a reference type directly; you must convert it to an object, e.g. by assigning it to a variable, parameter or property of typeobject
or casting it toobject
to make it a reference type.