Boxing: Converting a value type to reference type is called boxing
Boxing happens implicitly, where as unboxing is an explicit conversion.
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.
Unboxing: Converting a reference type to a value typpe is called unboxing. An unboxing operation consists of:
- Checking the object instance to make sure that it is a boxed value of the given value type.
- Copying the value from the instance into the value-type variable.
Attempting to unbox null causes a NullReferenceException. Attempting to unbox a reference to an incompatible value type causes an InvalidCastException.