当引用类的值类型成员时是否会发生拆箱?

发布于 2024-12-26 04:32:16 字数 468 浏览 2 评论 0原文

我读了 什么是装箱和拆箱?权衡是什么? 但无法理解一件事。假设我有一个类:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

榕城若虚 2025-01-02 04:32:16

停止考虑堆栈;这是完全错误的思考方式。显然,“装箱”的意思不是“在堆上”,因此任何“在堆上”的东西都必须是“装箱”的。

栈和堆是无关的。相反,请考虑引用当值类型的值必须被视为对对象的引用时,它会被装箱。如果您需要对值类型的值的引用,则创建一个框,将值放入框,并参考该框。现在您有了对值类型的值的引用。

不要将其与引用值类型的变量相混淆;那是完全不同的。变量和值是两个截然不同的东西;要引用变量,您可以使用“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.

铃予 2025-01-02 04:32:16

装箱或拆箱与在堆或堆栈上存储值没有任何关系。您应该阅读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.

一向肩并 2025-01-02 04:32:16

它既不是拆箱也不是装箱
考虑到您分配给 int 不带强制转换,并且我希望这段代码能够编译,这意味着 cls.Value 是一个 Integer( int) 类型。因此将 int 分配给 int
这里发生的是一个值副本

It's neither unboxing nor boxing.
Considering you assign to int without cast and, I hope, this code compiles, that means that cls.Value is a Integer(int) type. So assign int to int.
What happens here is a value copy.

断爱 2025-01-02 04:32:16
int i = 5;
object o = i;   // boxing of int i
int i = (int)o; // unboxing of object o

请注意,我们不会将 i 分配给对象的字段或属性,而是分配给对象本身。
它可以与光的性质相媲美。光可以被认为是由粒子(光子)组成的,或者是一种波。 int 可以是 int 对象(引用类型)或 int 值类型。但是,您不能直接将 int 定义为引用类型;您必须将其转换为对象,例如将其分配给object类型的变量、参数或属性,或将其转换为object以使其成为引用类型。

int i = 5;
object o = i;   // boxing of int i
int i = (int)o; // unboxing of object o

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 an int object (a reference type) or an int value type. You can however not define an int 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 type object or casting it to object to make it a reference type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文