Object type boxing with a reference type variable

2019-02-10 01:57发布

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?)

5条回答
来,给爷笑一个
2楼-- · 2019-02-10 02:45

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.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-02-10 02:49

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.

查看更多
爷的心禁止访问
4楼-- · 2019-02-10 02:49

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
查看更多
唯我独甜
5楼-- · 2019-02-10 02:58

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"
查看更多
放荡不羁爱自由
6楼-- · 2019-02-10 03:01

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 { ... }
查看更多
登录 后发表回答