gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 18:01:12 25 4
gpt4 key购买 nike

装箱是指将值类型分配给对象类型。将引用类型赋值给对象时是否一样?

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

    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 value of reference type is assigned to a variable of type object?

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

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

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

Is that boxing too?

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

关于c# - 带有引用类型变量的对象类型装箱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9092961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com