Object type boxing with a reference type variable
Asked Answered
A

5

17

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?)
Alemannic answered 1/2, 2012 at 8:41 Comment(1)
Like Henk says, all types are objects, and reference types are stored on the heap anyway.Oira
S
13

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 { ... }
Setula answered 1/2, 2012 at 8:43 Comment(0)
C
30

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.

Combs answered 1/2, 2012 at 16:23 Comment(2)
Is it still considered a "conversion" when there is no data/type converting involved - that is, when a conversion operator isn't required/called during the assignment, of say object obj = new MyClass();?Inchmeal
@sαmosΛris: That is an implicit reference conversion.Combs
S
13

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 { ... }
Setula answered 1/2, 2012 at 8:43 Comment(0)
S
2

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.

Seafaring answered 1/2, 2012 at 8:48 Comment(1)
Why does the reference have to be on the stack? Can't the reference be on the heap, or in a register?Combs
C
1

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"
Counterblast answered 5/2, 2012 at 18:12 Comment(0)
I
0

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
Inchmeal answered 4/6, 2018 at 20:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.