private void button2_Click(object sender, EventArgs e) { MyClass[] ary1 = new MyClass[5] { new MyClass(1, 12.34, "一二三四"), new MyClass(2, 23.45, "二三四五"), new MyClass(3, 34.56, "三四五六"), new MyClass(4, 45.67, "四五六七"), new MyClass(5, 56.78, "五六七八") }; MyClass2 o1 = new MyClass2(111, 1234.5678, "我是中文字。", new string[] { "中1111", "中2222", "中3333" }, ary1); // backup; //MyClass2 c1 = (MyClass2)o1.Clone(); MyClass2 c1 = o1.Clone2(); //MyClass2 c1 = o1.DeepClone(); // dump o1. txtMessage.AppendText(o1.ToString() + "\r\n"); // do change. o1.a = 222; o1.d = 2345.6789; o1.s = "我改變了"; o1.ss[1] = "我不再是我了"; ary1[0].a = 999; ary1[0].d = 1234.5678; ary1[0].s = "我改變了"; // restore o1 = c1; // dump o1. txtMessage.AppendText(o1.ToString() + "\r\n"); }
/// <remarks> /// 不可靠的clone方式。 /// </remarks> public MyClass2 Clone2() { MyClass2 c = new MyClass2(this.a, this.d, this.s, this.ss, this.ary1); c.ss = (string[])this.ss.Clone(); c.ary1 = (MyClass[])this.ary1.Clone(); return c; }