About boxing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tony Johansson

    About boxing

    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


  • Jon Skeet [C# MVP]

    #2
    Re: About boxing

    Tony Johansson <johansson.ande rsson@telia.com wrote:
    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.

    --
    Jon Skeet - <skeet@pobox.co m>
    Web site: http://www.pobox.com/~skeet
    Blog: http://www.msmvps.com/jon.skeet
    C# in Depth: http://csharpindepth.com

    Comment

    • Peter Duniho

      #3
      Re: About boxing

      On Tue, 03 Jun 2008 13:05:49 -0700, Tony Johansson
      <johansson.ande rsson@telia.com wrote:
      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 "concatenat e" 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

      Comment

      • Tony Johansson

        #4
        Re: About boxing

        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" <NpOeStPeAdM@nn owslpianmk.coms krev i meddelandet
        news:op.ub6vv6c f8jd0ej@petes-computer.local. ..
        On Tue, 03 Jun 2008 13:05:49 -0700, Tony Johansson
        <johansson.ande rsson@telia.com wrote:
        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 "concatenat e" 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


        Comment

        • Peter Duniho

          #5
          Re: About boxing

          On Tue, 03 Jun 2008 13:22:33 -0700, Tony Johansson
          <johansson.ande rsson@telia.com wrote:
          The reason for i to be boxed is because the second parameter of Writeln
          is
          received as an object.
          It's "WriteLine" . But yes, for the overload that applies to the syntax
          you've used, that's exactly right. There's not a WriteLine(Strin g, Int32)
          overload, so the one where i can be boxed as an object and passed that way
          winds up being used.

          Pete

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: About boxing

            Tony Johansson <johansson.ande rsson@telia.com wrote:
            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.

            --
            Jon Skeet - <skeet@pobox.co m>
            Web site: http://www.pobox.com/~skeet
            Blog: http://www.msmvps.com/jon.skeet
            C# in Depth: http://csharpindepth.com

            Comment

            • Cor Ligthert[MVP]

              #7
              Re: About boxing

              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.Writeln K(
              "Tony Johansson" <johansson.ande rsson@telia.com schreef in bericht
              news:xuh1k.7981 $R_4.6580@newsb .telia.net...
              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
              >

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: About boxing

                On Jun 4, 5:10 am, "Cor Ligthert[MVP]" <notmyfirstn... @planet.nl>
                wrote:
                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.WriteLi ne(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.WriteLi ne.
                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

                Comment

                Working...