Boxing and UnBoxing in C#?
-
Converting value type into reference is Boxing, and vice-varsa is UnBoxing.
-
Converting value type into reference is Boxing, and vice-varsa is UnBoxing.
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
-
Converting value type into reference is Boxing, and vice-varsa is UnBoxing.
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
-
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
-
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
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 |
-
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.
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
-
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 |
-
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
-
Converting value type into reference is Boxing, and vice-varsa is UnBoxing.
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].
-
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
-
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.
Thank you. Now I get it. But I have one question. Does this reference conversion consume much time as unboxing?
Eslam Afifi
-
Thanks a lot. Please have a look at this question.http://www.codeproject.com/script/Forums/View.aspx?fid=1649&msg=2434188
Eslam Afifi
-
Thank you. Now I get it. But I have one question. Does this reference conversion consume much time as unboxing?
Eslam Afifi
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.
-
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.
Ok. Thank you so much.
Eslam Afifi