带有引用类型变量的对象类型装箱

发布于 2025-01-01 22:23:20 字数 278 浏览 0 评论 0原文

装箱是指将值类型分配给对象类型。将引用类型分配给对象时是否相同?

当分配类型(不是对象)时,会发生什么?这也是拳击吗?

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

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

发布评论

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

评论(5

烟花肆意 2025-01-08 22:23:20

装箱是将值类型分配给对象类型的过程。

关闭。当值类型的值转换引用类型时,就会发生“装箱”。

引用类型的值赋给对象类型的变量是一样的吗?

不会。当值类型的值转换为引用类型时,就会发生装箱。将引用类型的值转换为对象不是装箱转换,而是引用转换。

当将引用类型(不是对象)的值分配给对象类型的变量时,会发生什么?

引用类型的值是引用。当将引用分配给对象类型的变量时,会在与该变量关联的存储位置中创建该引用的副本。

这也是拳击吗?

不会。当值类型的值转换为引用类型时,就会发生装箱。将引用类型的值转换为对象不是装箱转换,而是引用转换。

Boxing is when a value type is assigned to an object type.

Close. "Boxing" happens when a value of value type is converted to a reference type.

Is it the same when a value of reference type is assigned to a variable of type object?

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.

When a value of reference type (which isn't object) is assigned to a variable of type object, what happens?

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.

Is that boxing too?

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.

扛刀软妹 2025-01-08 22:23:20

我假设您的意思是这样的:

string s = "hello";
object x = s;        // no boxing, just implict conversion to base-type.

This Works 因为 System.String 与所有其他类一样,派生自 System.Object

public sealed class String : Object { ... }

I assume you mean something like

string s = "hello";
object x = s;        // no boxing, just implict conversion to base-type.

This works because System.String, like all other classes, derives from System.Object:

public sealed class String : Object { ... }
甲如呢乙后呢 2025-01-08 22:23:20

装箱是在堆栈上创建一个对象引用,该对象引用在堆上引用类型(例如 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.

盗心人 2025-01-08 22:23:20

Eric的答案符合CLI(公共语言基础设施)标准ECMA-335,分区 I(体系结构),第 5 章(术语和定义),它将装箱定义为:“将具有某种值类型的值转换为引用类型 System.Object 的新分配实例。”,以及拆箱为:“将具有类型 System.Object(其运行时类型是值类型)的值转换为值类型实例。”

CIL(通用中间语言)的装箱和拆箱指令的行为如下,这也是在 C# 或 VB.NET 上下文中谈到装箱/拆箱时通常隐含的含义。

然而,术语“装箱”和“拆箱”有时具有更广泛/实用的含义。例如,F# box 和 unbox 运算符可以进行值类型的转换 System.Object 之间的引用类型:

> let o = box "Hello World";;
val o : obj = "Hello World"
> let s:string = unbox o;;
val s : string = "Hello World"

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:

> let o = box "Hello World";;
val o : obj = "Hello World"
> let s:string = unbox o;;
val s : string = "Hello World"
淡写薰衣草的香 2025-01-08 22:23:20

将提供的代码编译为工作可执行文件并反汇编它,会显示第一个赋值 (obj) 的显式框指令,而第二个赋值 (obj2) 则不存在该指令:

来源

namespace BoxingAndTypeConversion
{
    class Program
    {
        public class MyClass { }

        static void Main(string[] args)
        {
            int num = 5;
            object obj = num;  //boxing
            //////////////////////
            MyClass my = new MyClass();
            object obj2 = my; //what is name this convert  (whethere is boxing?)
        }
    }
}

CIL

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       19 (0x13)
  .maxstack  1
  .locals init ([0] int32 num,
           [1] object obj,
           [2] class BoxingAndTypeConversion.Program/MyClass my,
           [3] object obj2)
  IL_0000:  nop
  IL_0001:  ldc.i4.5
  IL_0002:  stloc.0
  IL_0003:  ldloc.0
  IL_0004:  box        [mscorlib]System.Int32
  IL_0009:  stloc.1
  IL_000a:  newobj     instance void BoxingAndTypeConversion.Program/MyClass::.ctor()
  IL_000f:  stloc.2
  IL_0010:  ldloc.2
  IL_0011:  stloc.3
  IL_0012:  ret
} // end of method Program::Main

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

namespace BoxingAndTypeConversion
{
    class Program
    {
        public class MyClass { }

        static void Main(string[] args)
        {
            int num = 5;
            object obj = num;  //boxing
            //////////////////////
            MyClass my = new MyClass();
            object obj2 = my; //what is name this convert  (whethere is boxing?)
        }
    }
}

CIL

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       19 (0x13)
  .maxstack  1
  .locals init ([0] int32 num,
           [1] object obj,
           [2] class BoxingAndTypeConversion.Program/MyClass my,
           [3] object obj2)
  IL_0000:  nop
  IL_0001:  ldc.i4.5
  IL_0002:  stloc.0
  IL_0003:  ldloc.0
  IL_0004:  box        [mscorlib]System.Int32
  IL_0009:  stloc.1
  IL_000a:  newobj     instance void BoxingAndTypeConversion.Program/MyClass::.ctor()
  IL_000f:  stloc.2
  IL_0010:  ldloc.2
  IL_0011:  stloc.3
  IL_0012:  ret
} // end of method Program::Main
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文