newbie boxing question

J

Jacob

Hi All,


My C# book says that this code

string s = 10.ToString();

will box the literal 10 to an object (i.e. allocate a temporary class on the
heap). However, if I view this in assembly view (vs2005) I get something
like this:


int i = 5;

00000057 mov dword ptr [ebp-40h],5

string s1 = i.ToString();

0000005e lea ecx,[ebp-40h]

00000061 call 786FD550

00000066 mov esi,eax

00000068 mov dword ptr [ebp-44h],esi

string s2 = 6.ToString();

0000006b mov dword ptr [ebp-68h],6

00000072 lea ecx,[ebp-68h]

00000075 call 786FD550

0000007a mov esi,eax

0000007c mov dword ptr [ebp-48h],esi



I don't see any allocation in here at all. The literal 6 conversion seems to
look exactly like the int conversion that I put in for comparison. Where is
the boxing allocation happening? What am I missing?
 
A

A.Kahtava

the statement:

sting s = 10.ToString()

is treating a literal value as though it is an object, it is not
demonstrating boxing / unboxing.

to box an object you would do something like:

int i = 20;
object o = i; //Boxing
int j = (int)o; //Unboxing
 
A

A.Kahtava

Yury; are you referring to Jacob or myself?

Jacob was probably using the book "Professional C#" from Wrox.
I remember seeing the "10.ToString()" example associated with Boxing /
UnBoxing. In the context of the book, the 10.ToString() example leads
into boxing and unboxing. It's not an explicit example.

Links containing similar examples include:
http://www.programmersheaven.com/articles/arun/boxing.htm
http://www.codeproject.com/csharp/BoxUnBox.asp
http://www.c-sharpcorner.com/Code/2003/July/BoxNUnboxSSF.asp
http://www.codersource.net/csharp_boxing_unboxing.html
 
J

Jacob

Thanks, for your answer A.Kahtava, I was a little confused.

You are correct about the book too. But in the 2nd edition it gives that
example, and then a little later states

"Basically, the runtime creates a temporary reference-type "box" for the
object on the heap. This conversion can occur implitly, as in the example
above..."
 
C

Chris Dunaway

I don't think that example causes the integer to be boxed. Compare
this code:

Private Sub Foo()
Dim s As String
Dim i As Integer = 10
Dim j As Integer

s = i.ToString

Dim o As Object = i

j = DirectCast(o, Integer)

End Sub

To the IL generated for this code. Notice that the conversion of the
integer to string does not involve a box operation but when the integer
is assigned to an object and then cast back to integer a box and unbox
operation occurs.

..method private instance void Foo() cil managed
{
.maxstack 1
.locals init (
int32 num1,
int32 num2,
object obj1,
string text1)
L_0000: nop
L_0001: ldc.i4.s 10
L_0003: stloc.0
L_0004: ldloca.s num1
L_0006: call instance string int32::ToString()
L_000b: stloc.3
L_000c: ldloc.0
L_000d: box int32
L_0012: stloc.2
L_0013: ldloc.2
L_0014: unbox int32
L_0019: ldobj int32
L_001e: stloc.1
L_001f: nop
L_0020: ret
}
 
J

Jon Skeet [C# MVP]

Jacob said:
My C# book says that this code

string s = 10.ToString();

will box the literal 10 to an object (i.e. allocate a temporary class on the
heap).

There's no boxing going on here *because Int32 overrides ToString*. If
you try the following, it *does* involve boxing:

struct Foo
{
}

class Test
{
static void Main()
{
Foo f = new Foo();
f.ToString();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top