C# Interview questions on Boxing and Unboxing

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.


9 comments:

  1. Boxing is implicit... ex.
    int 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..

    ReplyDelete
  2. pls clarify mr venkat
    int 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;

    ReplyDelete
  3. in int i = 101;
    object 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

    ReplyDelete
  4. i think Boxing is
    int i=101;
    object obj=(object)i;// it is readable and suitable
    and UnBoxing is

    i=(int)obj;
    result is
    101

    ReplyDelete
  5. What's the actual use of BOxing and UnBoxing in real time application ??

    ReplyDelete
  6. More than it being of particular use, it's important to know when this happens because it can lead to performance implications.

    Also,
    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.

    ReplyDelete
  7. Hi Venkat

    Please clarify how boxing is implicit.Thanks

    ReplyDelete
  8. Boxing can be implicit or explicit.
    Unboxing can only be explicit.
    All the examples in comments above are pretty much explaining this.

    ReplyDelete
  9. 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
    int i=10;
    object o = i;

    ReplyDelete

If you are aware of any other C# questions asked in an interview, please post them below. If you find anything missing or wrong, please feel free to correct by submitting the form below.

 
Disclaimer - Terms of use - Contact Us