为什么结构需要进行盒装? [英] Why do structs need to be boxed?

查看:144
本文介绍了为什么结构需要进行盒装?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,任何用户定义的结构是自动的<击> System.Struct System.ValueType 和<击> System.Struct System.ValueType System.Object的

In C#, any user-defined struct is automatically a subclass of System.Struct System.ValueType and System.Struct System.ValueType is a subclass of System.Object.

但是,当我们分配一些结构对象类型引用它被装箱。例如:

But when we assign some struct to object-type reference it gets boxed. For example:

struct A
{
    public int i;
}

A a;
object obj = a;  // boxing takes place here

所以我的问题是:如果 A System.Object的的后裔,不能编译了-cast其对象类型,而不是拳击?

So my question is: if A is an descendant of System.Object, can't the compiler up-cast it to object type instead of boxing?

推荐答案

一个结构是值类型。 System.Object的是引用类型。值类型和引用类型的存储和运行​​时区别对待。对于一个值类型为引用类型进行治疗,有必要为它被装箱。从低水平的角度来看,这包括从那里它最初住在堆上,其中还包含一个对象标头中的新分配的存储器堆栈复制值。附加头是必要的引用类型,以解决他们的虚函数表,以使虚拟方法调度和其他引用类型相关的功能(请记住,在堆栈结构只是一个价值,它具有零类型信息;它不包含虚函数表一样东西并能'T可以直接用于动态解决调度方法)。此外,对待事情为引用类型,你必须有一个的参考的(指针)它,而不是它的原始值。

A struct is a value type. System.Object is a reference type. Value types and reference types are stored and treated differently by the runtime. For a value type to be treated as a reference type, it's necessary for it to be boxed. From a low level perspective, this includes copying the value from the stack where it originally lives to the newly allocated memory on the heap, which also contains an object header. Additional headers are necessary for reference types to resolve their vtables to enable virtual method dispatches and other reference type related features (remember that a struct on stack is just a value and it has zero type information; it doesn't contain anything like vtables and can't be directly used to resolve dynamically dispatched methods). Besides, to treat something as a reference type, you have to have a reference (pointer) to it, not the raw value of it.

所以我的问题是 - ?如果A是System.Object中的后裔,不能编译它上溯造型对象类型,而不是拳击

So my question is - if A is an descendant of System.Object, can't compiler upcast it to object type instead of boxing?

在一个较低的水平,值不继承任何东西。事实上,正如我以前说过,这不是一个真正的对象。是A派生自事实 System.ValueType 反过来从 System.Object的派生处于抽象级别上定义的东西你的编程语言(C#)和C#的确实是从您pretty以及隐藏装箱操作。你不提任何明确地框中的值,所以你可以简单地认为编译器upcasted结构为您服务。它使继承和多态的值幻觉,而没有任何的多态行为所需的工具直接由他们提供的。

At a lower level, a value does not inherit anything. Actually, as I said before, it's not really an object. The fact that A derives from System.ValueType which in turn derives from System.Object is something defined at the abstraction level of your programming language (C#) and C# is indeed hiding the boxing operation from you pretty well. You don't mention anything explicitly to box the value so you can simply think the compiler has "upcasted" the structure for you. It's making the illusion of inheritance and polymorphism for values while none of the tools required for polymorphic behavior is directly provided by them.

这篇关于为什么结构需要进行盒装?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆