Home >Backend Development >C#.Net Tutorial >C# implements a simple example of boxing and unboxing operations
This article uses a simple example to describe the implementation method of C# boxing and unboxing operations. Simply put, boxing is to convert the value type into a reference type; unboxing is to convert the reference type into a value type, which involves the stack and Anyone who has learned C# should know about the use of the heap, so I won’t be too embarrassed here. This example code is also for C# novices and is very simple.
The specific implementation code is as follows:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace UnBoxing { class Program { static void Main(string[] args) { int i = 123;//声明一个int类型的变量i,并初始化为123 object obj = i; //执行装箱操作 Console.WriteLine("装箱操作:值为{0},装箱之后对象为{1}", i, obj); int j = (int)obj;//执行拆箱操作 Console.WriteLine("拆箱操作:装箱对象为{0},值为{1}", obj, j); Console.ReadLine(); } } }
For more C# simple examples of boxing and unboxing operations, please pay attention to the PHP Chinese website!