装箱和拆箱
class Program { static void Main(string[] args) { int num = 10; //下边是装箱,是在堆中开辟一个空间存储num,不是简单的赋值 object o = num; //下边是拆箱,就是从堆中把数据取出来,在转化赋值给栈中的num2 int num2 = (int)o; Mys s=new Mys(); Myc c=new Myc(); s.num1 = 10; s.myc = new Myc(); s.myc.num2 = 10; //装箱 object oi = s; s.num1=20; s.myc.num2=20; // 拆箱 Mys s1=(Mys)oi; Console.WriteLine("si.num1:{0}\ns1.myc.num2:{1}", s1.num1, s1.myc.num2); Console.ReadKey(); } } struct Mys { public int num1; public Myc myc; } class Myc { public int num2; }
图解
我叫小小菜,想要成为一棵大大包心菜.