About boxing

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

Hello!

int i = 5;
if I write this statement
Console.Writeln("Test of i = {0}", i);
then I assume that will i be boxed
Is that correct understood of me?

//Tony
 
Tony Johansson said:
int i = 5;
if I write this statement
Console.Writeln("Test of i = {0}", i);
then I assume that will i be boxed
Is that correct understood of me?

Yes.
 
Hello!

The reason for i to be boxed is because the second parameter of Writeln is
received as an object.
Is this also correct understodd of me?

//Tony



"Peter Duniho" <[email protected]> skrev i meddelandet
Hello!
Hi!

int i = 5;
if I write this statement
Console.Writeln("Test of i = {0}", i);
then I assume that will i be boxed
Is that correct understood of me?

Yes. You can avoid the boxing penalty by making the needed string
conversion happen earlier. Either just "concatenate" the variable i to
the "Test of i = " string (the compiler will perform the type conversion)
or call i.ToString() and pass that as your argument instead of i.

That said, i/o is slow. It's unlikely that the cost of boxing an argument
to the WriteLine() method would be a problem. IMHO, the code you posted
is perfectly fine (just fix the name of the method so it compiles :) ).

Pete
 
Tony Johansson said:
The reason for i to be boxed is because the second parameter of Writeln is
received as an object.
Is this also correct understodd of me?

Yup.
 
Tony,

In my idea is this not boxing.

For me is Boxing is putting an object in an extra object like a box.

However here is behind the scene probably the already overload function
ToString() method from system.values used.

Be aware that beside of the thoughts of some C++ programmers who were busy
on 8088 (or even 16 bits ones) the effect from boxing is today very low on
your performance as it is not about things as pixel processing or more like
that.

Cor


Console.WritelnK(
 
In my idea is this not boxing.

Consult the generated IL and you'll find it certainly is.
For me is Boxing is putting an object in an extra object like a box.

And that's what happens when you call Console.WriteLine(String,
Object).

If there were an overload for WriteLine which took (String,int) then
it wouldn't be boxed at that point (but possibly later). In this case,
however, the value of i is boxed as part of the act of making the call
to Console.WriteLine.
Be aware that beside of the thoughts of some C++ programmers who were busy
on 8088 (or even 16 bits ones) the effect from boxing is today very low on
your performance as it is not about things as pixel processing or more like
that.

Agreed.

Jon
 

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

Back
Top