What is Boxing and Unboxing?
Boxing - Converting a value type to reference type is called boxing. An example is shown below.
int i = 101;
object obj = (object)i; // Boxing
Unboxing - Converting a reference type to a value typpe is called unboxing. An example is shown below.
obj = 101;
i = (int)obj; // Unboxing
Is boxing an implicit conversion?
Yes, boxing happens implicitly.
Is unboxing an implicit conversion?
No, unboxing is an explicit conversion.
What happens during the process of boxing?
Boxing is used to store value types in the garbage-collected heap. Boxing is an implicit conversion of a value type to the type object or to any interface type implemented by this value type. Boxing a value type allocates an object instance on the heap and copies the value into the new object. Due to this boxing and unboxing can have performance impact.
Subscribe to:
Post Comments (Atom)
Boxing is implicit... ex.
ReplyDeleteint i = 101;
object obj = (object)i; // Boxing
or
u can also write it as
object obj = i; // Point that we did not type cast to object.. No need in boxing .. therefore it is implicit.
While in unboxing we need to type cast.. therefore it is explicit..
pls clarify mr venkat
ReplyDeleteint i = 101;
object obj = (object)i; // Boxing ->here ur type cast to integer to object type this explicit conversion i think.
int i = 101;
object o = i;
in int i = 101;
ReplyDeleteobject obj = (object)i; it refers to the explicit(user defined) type cast where as in second condition int i= 101;
object o = i; this is implicit type cast....actualy first 1 cud also be written like second 1 but in this case we have option but when we perform the type casting from object to any data type then we must have to define the datatype of that casting
because in dot net there are 16 different datatypes so the compiler wud be confused when it will cast the value from object to any datatype(Like char ,double,float int Int32 Int16 etc) in this case we must explicitly cast this value.....
Further Exmple:
int i = 100;
object ob = i;// type cast from int to object
//compiler can compile the code because it knws the object is for anonymous/any datatype.......
but when
int i = ob; // this would be wrong because now compiler dont know that which datatype wud this object contain???(either float??? int??? char??? etc) so we must explicitly define the datatype here for casting this object to the require datatype like below
int i = (int)ob;
& this is called unboxing
hope somthing is clear now :)
regards Aoun Ali
i think Boxing is
ReplyDeleteint i=101;
object obj=(object)i;// it is readable and suitable
and UnBoxing is
i=(int)obj;
result is
101
What's the actual use of BOxing and UnBoxing in real time application ??
ReplyDeleteMore than it being of particular use, it's important to know when this happens because it can lead to performance implications.
ReplyDeleteAlso,
int i=101;
object obj=(object)i;// is Boxing, but it's explicit. You are explicitly casting to object
int i=101;
object obj=i; also works, and here the boxing is implicit.
Hi Venkat
ReplyDeletePlease clarify how boxing is implicit.Thanks
Boxing can be implicit or explicit.
ReplyDeleteUnboxing can only be explicit.
All the examples in comments above are pretty much explaining this.
Boxing is implicit conversion and object type is able to store any type of variable and hence integer can easily assigned to object type without any conversion so you can write easily
ReplyDeleteint i=10;
object o = i;