Tuesday 22 March 2011

Boxing and unboxing interview questions

What do you mean by boxing and un-boxing?
C# provides us with Value types and Reference Types. Value Types are stored on the stack and Reference types
are stored on the heap. The conversion of value type to reference type is known as boxing and converting
reference type back to the value type is known as un-boxing.
e.g.
int x = 100;

object o = x ;  //  Implicit boxing
object o = (object) x; // Explicit Boxing

x = o; // Implicit Un-Boxing
x = (int)o; // Explicit Un-Boxing


What happens during the process of boxing?
Boxing is used to store value types data 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.During this an object is created on the heap and
value is copied over their.



No comments:

Post a Comment