Describe Boxing and Unboxing.Boxing - It allows you to treat a value type the same as a reference type. - It is used to store value types in the garbage-collected heap. - It is an implicit conversion of a value type to the type object or to any interface type implemented by this value type. - A value type allocates an object instance on the heap and copies the value into the new object.
Unboxing - It converts a boxed reference type back to a value type. - It is an explicit conversion from the type object to a value type or from an interface type to a value type that implements the interface.
An unboxing operation consists of:
1. Checking the object instance to make sure that it is a boxed value of the given value type. 2. Copying the value from the instance into the value-type variable.
|