Friday, April 18, 2008

What is Boxing and unboxing? How Boxing and unboxing occures in memory?  Why only boxed types can be unboxed?

Boxing – Process of converting a System.ValueType to Reference Type , Mostly base class System.Object type and allocating it memory on Heap .Reverse is unboxing , but can only be done with prior boxed variables.
Boxing is always implicit but Unboxing needs to be explicitly done via casting , thus ensuring the value type contained inside .

Boxing converts value type to reference type , thus allocating memory on Heap . Unboxing converts already boxed reference types to value types through explicit casting , thus allocating memory on stack .

Unboxing is the process of converting a Reference type variable to Value type and thus allocating memory on the stack . It happens only to those Reference type variables that have been earlier created by Boxing of a Value Type , therefore internally they contain a value type , which can be

No comments: