Boxing
It is the conversion of value type to object type. It makes the value type to be stored on garbage collected heap. Boxing a value type allocates an object instance on the heap and copies the value into the new object.
When we are saving values to an arraylist we need to box the value type (like int, string etc) to reference type(object);
For example:
int I = 0;
Object o = (object) I;
Unboxing
It is the reverse of boxing. It converts reference types to value types.
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.
For example:
int i = 123; // a value type
object o = i; // boxing
int j = (int)o;
No comments:
Post a Comment