boxing when calling ToString on a struct object

T

Tony Johansson

Hello!

Here is a simple program that I have used just for this question.
One thing that is strange is when I for example have this statement
Console.WriteLine(car.ToString());
in main. Here for some reason boxing occurs before the ToString method get
called in the root object
I wonder why ?


static void Main(string[] args)
{
Car car = new Car("Test");
Console.WriteLine(car.ToString());
}

public struct Car
{
string name;

public Car(string name)
{
this.name = name;
}
}

//Tony
 
A

Anthony Tolle

Hello!

Here is a simple program that I have used just for this question.
One thing that is strange is when I for example have this statement
Console.WriteLine(car.ToString());
in main. Here for some reason boxing occurs before the ToString method get
called in the root object
I wonder why ?

//Tony

Perhaps because you haven't defined an explicit ToString method for
Car? (e.g. it is forced to use object.ToString)
 
P

Peter Duniho

Tony said:
Hello!

Here is a simple program that I have used just for this question.
One thing that is strange is when I for example have this statement
Console.WriteLine(car.ToString());
in main. Here for some reason boxing occurs before the ToString method get
called in the root object
I wonder why ?

What makes you think boxing occurs?

I haven't had a chance to verify your concern, but it would surprise me
if any boxing was necessary.

Pete
 
T

Tony Johansson

Peter Duniho said:
What makes you think boxing occurs?

I haven't had a chance to verify your concern, but it would surprise me if
any boxing was necessary.

Pete

Hi

It says so in the book that is part of the preparation for the exam that I
use and here also
http://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.constrained.aspx

In the book it says.
Boxing also occurs when you call virtual method that a structure inherit
from System.Object, such as ToString

//Tony
 
P

Peter Duniho

Tony said:
It says so in the book that is part of the preparation for the exam that I
use and here also
http://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.constrained.aspx

In the book it says.
Boxing also occurs when you call virtual method that a structure inherit
from System.Object, such as ToString

Okay, seems pretty clear to me: because your value type class does not
implement ToString() itself, the IL emitted is required to box the value
type, so that the virtual base implementation can be called.

If your type had implemented the method, then because the compiler knows
the virtual method cannot be overridden by some other type (user-defined
value types can't be inherited), it can call the actual implementation
directly (no boxing). But if your type doesn't implement the method,
then the boxing is needed so that the virtual method call can work normally.

If you are wondering why the base implementation isn't simply called,
note this comment in the document page you referenced:

Using the constrained prefix also avoids potential versioning
problems with value types. If the constrained prefix is not
used, different IL must be emitted depending on whether or not
a value type overrides a method of System.Object. For example,
if a value type V overrides the Object.ToString() method, a
call V.ToString() instruction is emitted; if it does not, a
box instruction and a callvirt Object.ToString() instruction
are emitted. A versioning problem can arise in the former case
if the override is later removed, and in the latter case if an
override is later added.

In other words, the type could be changed at some point after when your
own calling code was compiled, adding an override to the virtual method
that didn't exist before. Making the virtual call through the boxed
type ensures that if that occurs, the new override is called as
expected, without requiring the calling code to be recompiled.

Hope that helps.

Pete
 

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

Similar Threads


Top