.NET contains a special Object class that can accept values of any data type. When the type passed or assigned is not a specific data type, the object class provides a general method for passing parameters and assigning values. . The value assigned to object must be of reference type and stored in the managed heap.
Boxing:
int age = 24;
object refAge= age;
As you can see, the first statement creates a variable age and places the value on the managed stack ;
The second statement assigns the value of age to the reference type. It places the value 24 in the managed heap.
The process of packaging this value type into a reference type is called boxing.
Unboxing:
Conversely, the process of converting a reference type to a value type is called unboxing. Unboxing will coerce the object to its original type. Unbox the previous object.
int newAge = (int) refAge;
string newAge =(String) refAge;
The unboxed value must have the same type as the variable it is being converted to.
Through the simple illustration above, do you have a general understanding of the principles of C# boxing and unboxing?
More C# boxing and unboxing For detailed explanation of the principles and related articles, please pay attention to the PHP Chinese website!