简体   繁体   中英

Object type boxing with a reference type variable

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

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.

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 { ... }

Boxing is creating an object reference, on the stack, that references a value of the type say for eg int, on the heap. But when a reference type (witch isn't object)assigned to object, it is not boxing.

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"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM