simple Boxing question

A

Andrew Gniadek

Hey,
just going through "C# for Java Developers" by Jones&Freeman and I've
got a quick question about one of their examples. They give a simple
example of converting an instance of a value type to an object:
// Box an int variable
int myInt = 100;
object myIntObject = myInt;
System.Console.WriteLine("myIntObject = " + myInt.ToString());

Just curious if what they meant to do is
System.Console.WriteLine("myIntObject = " + myIntObject.ToString());
because wouldn't calling ToString() on myInt just use implicit boxing
making the statement
object myIntObject = myInt;
unnecessary?

Thanks,
A.
 
N

Nicholas Paldino [.NET/C# MVP]

Andrew,

Yes, when the method call ToString is made, it implicitly boxes the int.
The myIntObject is unecessary.

Hope this helps.
 
C

cody

Yes, when the method call ToString is made, it implicitly boxes the int.

I do not believe that. I read that if one overrides the ToString() method of
a value type then calling ToString() will not box the struct. If I do not
override it and call the implementation defined in System.Object() implicit
boxing will occur.
 
C

C# Learner

Nicholas said:
Yes, when the method call ToString is made, it implicitly boxes the int.
The myIntObject is unecessary.

I've heard similar elsewhere too. Why is this needed? Why does a value
need to be stored on the heap before a string representation of an
integer can be obtained, here?
 
R

Richard A. Lowe

That's true: an explicit call to a struct method, even if it overrides an
object method, will not implicitly box. What Nicolas may be thinking of,
however, is a case like this:

static void Main()
{
int i=65536;
Console.WriteLine("This is boxed: {0}", i);
Console.WriteLine("This is not boxed: {1}", i.ToString());
}

In the first WriteLine the second argument expects and object and therefore
needs to implicitly box the int before calling the overridden ToString
method. In the second, by calling ToString yourself, you return a reference
type (a stirng) to the method (not a value type - so no boxing), so the
compiler doesn't need to box the int.

Looking at the IL of the above C# snippet supports that conclusion.
Personally I think if performance isn't the absolutely hyper-critical factor
in your app (which it isn't in 99.99% of apps), you should use the first
form for readability.

Richard
--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
cody said:
Yes, when the method call ToString is made, it implicitly boxes the int.

I do not believe that. I read that if one overrides the ToString() method of
a value type then calling ToString() will not box the struct. If I do not
override it and call the implementation defined in System.Object() implicit
boxing will occur.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
 
C

cody

Yes, when the method call ToString is made, it implicitly boxes the
int.
I've heard similar elsewhere too. Why is this needed? Why does a value
need to be stored on the heap before a string representation of an
integer can be obtained, here?

It isn't. please read the other posts.
 

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