//引用類型(由於使用了‘Class’) class SomeRef{public Int32 x;} //值類型(由於使用了‘Struct’) struct SomeVal{public Int32 x;} static void ValueTypeDemo(){ SomeRef r1 = new SomeRef(); //在堆上分配 SomeVal v1 = new SomeVal(); //在棧上分配 r1.x = 5; //提領指針 v1.x = 5; //在棧上修改 Console.WriteLine(r1.x); //顯示“5” Console.WriteLine(v1.x); //同樣顯示“5” SomeRef r2 = r1; //只復制引用(指針) SomeVal v2 = v1; //在棧上分配並復制成員 r1.x = 8; //r1.x和r2.x都會更改 v1.x = 9; //v1.x會更改,但v2.x不變 Console.WriteLine(r1.x); //顯示“8” Console.WriteLine(r2.x); //顯示“8” Console.WriteLine(v1.x); //顯示“9” Console.WriteLine(v2.x); //顯示“5” }