Packing and unpacking: any value type and reference type can be converted to the object type. Boxing conversion refers to implicit or explicit conversion of a value type into an object type, or convert the value type to an interface-type applied to the value type ). To pack a value of the value type, create an object instance and copy the value to the object. The data in the packed object is in the heap, and the address in the heap is in the stack. The value of the boxed type is assigned to the object as a copy. For example: int I = 10; object obj = I; // implicitly boxed object obj = object (I); // explicitly boxed if (obj is int) // int Console. writeLine ("OK ");
Console. WriteLine (obj. GetType (); // System. Int32
There are two ways to view the type of raw data packaged in the referenced object after packaging. To determine whether the original type is a given atomic type, use is. To return a string, use the GetType method of the object class.
Box-breaking conversion refers to explicitly converting an object type into a value type, or explicitly converting an interface type into a value type that executes this interface. Note that the packing operation can be performed implicitly, but the unpacking operation must be explicit. The unpacking process is divided into two steps: first, check the object instance to see if it is a boxed value of the given value type. Then, copy the Instance value to the value type variable. For example: int I = 10; object obj = I; int j = (int) obj; there are two ways to view the type of raw data packaged in the referenced object after packaging. To determine whether the original type is a given atomic type, use is. To return a string, use the GetType method of the object class.
Box-breaking conversion refers to explicitly converting an object type into a value type, or explicitly converting an interface type into a value type that executes this interface. Note that the packing operation can be performed implicitly, but the unpacking operation must be explicit. The unpacking process is divided into two steps: first, check the object instance to see if it is a boxed value of the given value type. Then, copy the Instance value to the value type variable. For example: int I = 10; object obj = I; int j = (int) obj;