装箱和拆箱是 C# 中的一个重要概念。 C# 类型系统包含三种数据类型:值类型(int、char 等) 、引用类型(对象)和指针类型。基本上,它将值类型转换为引用类型,反之亦然。装箱和拆箱实现了类型系统的统一视图,其中任何类型的值都可以被视为对象。
Boxing | Unboxing |
---|---|
It convert value type into an object type. | It convert an object type into value type. |
Boxing is an implicit conversion process. | Unboxing is the explicit conversion process. |
Here, the value stored on the stack copied to the object stored on the heap memory. | Here, the object stored on the heap memory copied to the value stored on the stack . |
Example:
Output:
|
Example:
Output:
|