带有引用类型变量的对象类型装箱
装箱是指将值类型分配给对象类型。将引用类型分配给对象时是否相同?
当分配类型(不是对象)时,会发生什么?这也是拳击吗?
int num=5;
object obj = num; //boxing
//////////////////////
MyClass my = new MyClass();
object obj = my; //what is name this convert (whethere is boxing?)
Boxing is when a value type is assigned to an object type. Is it the same when a reference type is assigned to an object?
When a type (which isn't object) is assigned, what happens? Is that boxing too?
int num=5;
object obj = num; //boxing
//////////////////////
MyClass my = new MyClass();
object obj = my; //what is name this convert (whethere is boxing?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
关闭。当值类型的值转换为引用类型时,就会发生“装箱”。
不会。当值类型的值转换为引用类型时,就会发生装箱。将引用类型的值转换为对象不是装箱转换,而是引用转换。
引用类型的值是引用。当将引用分配给对象类型的变量时,会在与该变量关联的存储位置中创建该引用的副本。
不会。当值类型的值转换为引用类型时,就会发生装箱。将引用类型的值转换为对象不是装箱转换,而是引用转换。
Close. "Boxing" happens when a value of value type is converted to a reference type.
No. Boxing happens when a value of value type is converted to a reference type. Converting a value of reference type to object is not a boxing conversion, it is a reference conversion.
A value of reference type is a reference. When a reference is assigned to a variable of type object, a copy of the reference is made in the storage location associated with the variable.
No. Boxing happens when a value of value type is converted to a reference type. Converting a value of reference type to object is not a boxing conversion, it is a reference conversion.
我假设您的意思是这样的:
This Works 因为
System.String
与所有其他类一样,派生自System.Object
:I assume you mean something like
This works because
System.String
, like all other classes, derives fromSystem.Object
:装箱是在堆栈上创建一个对象引用,该对象引用在堆上引用类型(例如 int)的值。但是当一个引用类型(不是对象)分配给对象时,它就不是装箱了。
Boxing is creating an object reference, on the stack, that references a value of the type say for e.g. int, on the heap. But when a reference type (witch isn't object)assigned to object, it is not boxing.
Eric的答案符合CLI(公共语言基础设施)标准ECMA-335,分区 I(体系结构),第 5 章(术语和定义),它将装箱定义为:“将具有某种值类型的值转换为引用类型 System.Object 的新分配实例。”,以及拆箱为:“将具有类型 System.Object(其运行时类型是值类型)的值转换为值类型实例。”
CIL(通用中间语言)的装箱和拆箱指令的行为如下,这也是在 C# 或 VB.NET 上下文中谈到装箱/拆箱时通常隐含的含义。
然而,术语“装箱”和“拆箱”有时具有更广泛/实用的含义。例如,F# box 和 unbox 运算符可以进行值类型的转换和 System.Object 之间的引用类型:
Eric's answer corresponds to the CLI (Common Language Infrastructure) standard ECMA-335, partition I (Architecture), chapter 5 (Terms and definitions), which defines boxing as: "The conversion of a value having some value type, to a newly allocated instance of the reference type System.Object.", and unboxing as: "The conversion of a value having type System.Object, whose run-time type is a value type, to a value type instance."
The box and unbox instructions of the CIL (Common Intermediate Language) behave like this, and this is also the meaning usually implied when speaking of boxing/unboxing in the context of C# or VB.NET.
However, the terms boxing and unboxing are sometimes used in a wider/pragmatic sense. For instance, the F# box and unbox operators can do conversions of value types and reference types to and from System.Object:
将提供的代码编译为工作可执行文件并反汇编它,会显示第一个赋值 (
obj
) 的显式框指令,而第二个赋值 (obj2
) 则不存在该指令:来源
CIL
Compiling the provided code into a working executable and disassembling it reveals an explicit box instruction for the first assignment (
obj
) that is not present for the second (obj2
):Source
CIL