Introduction
-
Boxing
Boxing is an implicit conversion procedure that changes value types (such as int, char, etc.) into reference types (such as objects).
-
Unboxing
Unboxing employs an explicit conversion mechanism to transform the reference type (object) to a value type (int, char, etc.).
Boxing conversions
Any value type may be implicitly converted to the type object or to any interface type that the value type implements through the use of a boxing conversion. Allocating an object instance and copying the value-type value into it is boxing a value of a value-type.
The boxing class, for instance, would be declared as follows for any value-type G:
class CodeB { Codevalue; CodeA(T t) { value = v; } }
The expression new CodeA(v) must now be used to box a value v of type T, and the resultant instance must be returned as a value of type object. as a result, the claims+
int count = 12; object value = i;
conceptually correspond to,
int count = 12; object obj = new int_obj(i);
CodeA and int Box, two hypothetical boxing classes, aren’t real, and a boxed value’s dynamic type isn’t a class type either. Instead, a type T boxed value has the dynamic type T, which makes it possible to reference type T in a dynamic type check using the is the operator.
For example
int count = 12; object obj = i; if (obj is int) { Console.Write("Obj contains an int"); }
The string “Obj contains an int” will be shown on the console using the code above.
Making a duplicate of the value being boxed is implied by the boxing conversion. In contrast, when a reference type is converted to a type object, the value keeps referencing the same instance and is only thought of as the less derived type object.
For example, given the declaration
struct Square { public int i, j; public Square(int i, int j) { this.i =i; this.j = j; } }
the following statements
Square p =new Square(10, 10); object obj = p; p.x = 20; Console.Write(((Square)obj ).x);
Due to the implicit boxing procedure that takes place when p is assigned to the box, which copies the value of p, they will print the value 10 on the console. The point would have produced the value 20 if it had been declared a class instead, as p and the box would have pointed to the same instance.
Unboxing conversions
An explicit conversion from a type object to any value type or from any interface type to any value type that implements the interface type is permitted by an unpacking conversion. In order to unbox an object instance, you must first verify that it contains a boxed value of the specified value type before copying the value from the instance.
Thus, the statements,
object obj= 12; int i = (int)obj;
conceptually correspond to,
object obj = new int_obj(12); int i = ((int_Obj)obj).value;
The value of the source argument must be a reference to an object that was previously constructed by boxing a value of that value type in order for an unpacking conversion to a particular value type to proceed at run-time. An InvalidCastException is triggered if the source parameter is null or a reference to an unsupported object.