Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Boxing and UnBoxing in C#?

Boxing and UnBoxing in C#?

Scheduled Pinned Locked Moved C#
csharpquestion
14 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ReenaSharma
    wrote on last edited by
    #1

    Converting value type into reference is Boxing, and vice-varsa is UnBoxing.

    R E E 3 Replies Last reply
    0
    • R ReenaSharma

      Converting value type into reference is Boxing, and vice-varsa is UnBoxing.

      R Offline
      R Offline
      Ravi Bhavnani
      wrote on last edited by
      #2

      ReenaSharma wrote:

      Converting value type into reference is Boxing

      Almost. :) You don't "convert" anything - boxing is the process of placing a primitive type in an object so that the primitive type can be used as an object. See this Wikipedia[^] link. /ravi

      My new year resolution: 2048 x 1536 Home | Music | Articles | Freeware ravib(at)ravib(dot)com

      1 Reply Last reply
      0
      • R ReenaSharma

        Converting value type into reference is Boxing, and vice-varsa is UnBoxing.

        E Offline
        E Offline
        Eslam Afifi
        wrote on last edited by
        #3

        let's say we have these 2 classes. Class B inherits from class A class A { } class B : A { } in the following code A obja; B objb = new B(); obja = objb; // boxing B objc = (B)obja; // unboxing We can say, Boxing is assigning an object of the child type to an object of the parent type (higher in the inheritance hierarchy) Unboxing is (casting) assigning an object of the parent type to an object of the child type. see also

        Eslam Afifi

        G C 2 Replies Last reply
        0
        • E Eslam Afifi

          let's say we have these 2 classes. Class B inherits from class A class A { } class B : A { } in the following code A obja; B objb = new B(); obja = objb; // boxing B objc = (B)obja; // unboxing We can say, Boxing is assigning an object of the child type to an object of the parent type (higher in the inheritance hierarchy) Unboxing is (casting) assigning an object of the parent type to an object of the child type. see also

          Eslam Afifi

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          Eslam Afifi wrote:

          Boxing is assigning an object of the child type to an object of the parent type

          No, that's not correct. There is no boxing going on there.

          Despite everything, the person most likely to be fooling you next is yourself.

          E 1 Reply Last reply
          0
          • E Eslam Afifi

            let's say we have these 2 classes. Class B inherits from class A class A { } class B : A { } in the following code A obja; B objb = new B(); obja = objb; // boxing B objc = (B)obja; // unboxing We can say, Boxing is assigning an object of the child type to an object of the parent type (higher in the inheritance hierarchy) Unboxing is (casting) assigning an object of the parent type to an object of the child type. see also

            Eslam Afifi

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            Eslam Afifi wrote:

            class A { } class B : A { } in the following code A obja; B objb = new B(); obja = objb; // boxing B objc = (B)obja; // unboxing

            Wrong! Boxing does not happen with instances of classes.

            Upcoming FREE developer events: * Developer Day Scotland Recent blog posts: * Different ways to add point data in SQL Server 2008 * Spatial References in SQL Server 2008 My website |

            E 1 Reply Last reply
            0
            • G Guffa

              Eslam Afifi wrote:

              Boxing is assigning an object of the child type to an object of the parent type

              No, that's not correct. There is no boxing going on there.

              Despite everything, the person most likely to be fooling you next is yourself.

              E Offline
              E Offline
              Eslam Afifi
              wrote on last edited by
              #6

              Maybe I'm wrong, but this is how I understand it, and I'll be grateful if you clarify this point to me, please.

              B objc = (B)obja;
              

              Isn't that unboxing?

              Eslam Afifi

              G P 2 Replies Last reply
              0
              • C Colin Angus Mackay

                Eslam Afifi wrote:

                class A { } class B : A { } in the following code A obja; B objb = new B(); obja = objb; // boxing B objc = (B)obja; // unboxing

                Wrong! Boxing does not happen with instances of classes.

                Upcoming FREE developer events: * Developer Day Scotland Recent blog posts: * Different ways to add point data in SQL Server 2008 * Spatial References in SQL Server 2008 My website |

                E Offline
                E Offline
                Eslam Afifi
                wrote on last edited by
                #7

                http://www.codeproject.com/script/Forums/View.aspx?fid=1649&msg=2433376

                Eslam Afifi

                1 Reply Last reply
                0
                • E Eslam Afifi

                  Maybe I'm wrong, but this is how I understand it, and I'll be grateful if you clarify this point to me, please.

                  B objc = (B)obja;
                  

                  Isn't that unboxing?

                  Eslam Afifi

                  G Offline
                  G Offline
                  Guffa
                  wrote on last edited by
                  #8

                  No, that's not unboxing. Unboxing is when you extract the value from a value type that is stored as an object. What you are doing is merely changing the type of a reference.

                  Despite everything, the person most likely to be fooling you next is yourself.

                  E 1 Reply Last reply
                  0
                  • R ReenaSharma

                    Converting value type into reference is Boxing, and vice-varsa is UnBoxing.

                    E Offline
                    E Offline
                    Erich Ledesma
                    wrote on last edited by
                    #9

                    Boxing is copying a value type (C# struct) to a reference object in the heap. Unboxing is copying a boxed object in the heap back to the stack. Not just primitive value types: struct S : ISomeInterface { ... } // boxing S s1 = new S() ; object x = s1 ; // S instance is created on the stack, then copied to the heap. ISomeInterface is = s1 ; // This is also boxing ... S s = (S)x ; // This is unboxing, for unboxing you must use the cast syntax. Value types are not always placed on the stack, they are inline into it's container. They might be already on the heap, for instance if they are array elements. S[] a = ... a[i] = (S)x ; Boxed value is copied from the heap to the stack and then copied into a[i].

                    1 Reply Last reply
                    0
                    • E Eslam Afifi

                      Maybe I'm wrong, but this is how I understand it, and I'll be grateful if you clarify this point to me, please.

                      B objc = (B)obja;
                      

                      Isn't that unboxing?

                      Eslam Afifi

                      P Offline
                      P Offline
                      Pete OHanlon
                      wrote on last edited by
                      #10

                      Have a look at this[^] entry in wikipedia.

                      Deja View - the feeling that you've seen this post before.

                      My blog | My articles

                      E 1 Reply Last reply
                      0
                      • G Guffa

                        No, that's not unboxing. Unboxing is when you extract the value from a value type that is stored as an object. What you are doing is merely changing the type of a reference.

                        Despite everything, the person most likely to be fooling you next is yourself.

                        E Offline
                        E Offline
                        Eslam Afifi
                        wrote on last edited by
                        #11

                        Thank you. Now I get it. But I have one question. Does this reference conversion consume much time as unboxing?

                        Eslam Afifi

                        G 1 Reply Last reply
                        0
                        • P Pete OHanlon

                          Have a look at this[^] entry in wikipedia.

                          Deja View - the feeling that you've seen this post before.

                          My blog | My articles

                          E Offline
                          E Offline
                          Eslam Afifi
                          wrote on last edited by
                          #12

                          Thanks a lot. Please have a look at this question.http://www.codeproject.com/script/Forums/View.aspx?fid=1649&msg=2434188

                          Eslam Afifi

                          1 Reply Last reply
                          0
                          • E Eslam Afifi

                            Thank you. Now I get it. But I have one question. Does this reference conversion consume much time as unboxing?

                            Eslam Afifi

                            G Offline
                            G Offline
                            Guffa
                            wrote on last edited by
                            #13

                            Eslam Afifi wrote:

                            Does this reference conversion consume much time as unboxing?

                            No. Reference conversion doesn't create a new object, it only verifies that the object can be used as the desired type and then copies the reference. If you convert from one known class to another, the verification can be done by the compiler, so the only thing that is done at runtime is copying the reference.

                            Despite everything, the person most likely to be fooling you next is yourself.

                            E 1 Reply Last reply
                            0
                            • G Guffa

                              Eslam Afifi wrote:

                              Does this reference conversion consume much time as unboxing?

                              No. Reference conversion doesn't create a new object, it only verifies that the object can be used as the desired type and then copies the reference. If you convert from one known class to another, the verification can be done by the compiler, so the only thing that is done at runtime is copying the reference.

                              Despite everything, the person most likely to be fooling you next is yourself.

                              E Offline
                              E Offline
                              Eslam Afifi
                              wrote on last edited by
                              #14

                              Ok. Thank you so much.

                              Eslam Afifi

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups