关于Clone
关于Clone的学习
前阵子看到blog中关于Clone的资料
整理下:
关于浅表CLONE,用一个指针指向该地址。也就是说至少有2个指针指向这个地址。
深度CLONE,就是新建一个,并复制原有的数据。克隆对象和原始对象地址肯定不一样。
本人用以下代码测试:
37
38 //测试点:
39 private void button2_Click(object sender, System.EventArgs e)
40 {
41 Person p = new Person();
42 p.PersonName = new Name();
43 p.PersonName.LastName = "jill";
44 p.PersonName.FirstName = "zhang";
45 p.Email = "jillzhang@126.com";
46 Person sameNamePerson = p.Clone() as Person;
47 sameNamePerson.ChangLastName("clr_");
48 this.TestInfo.Text += "深度克隆原始数据:" + p.PersonName.LastName + "\r\n";
49 this.TestInfo.Text += "深度克隆数据:" + sameNamePerson.PersonName.LastName + "\r\n";
50
51 this.TestInfo.Text += "浅表克隆原始数据:" + p.PersonName.LastName + "\r\n";
52 Person samePerson = p.ReturnOne();
53 samePerson.ChangLastName("Shadow");
54
55
56 this.TestInfo.Text += "浅表克隆数据:" + samePerson.PersonName.LastName + "\r\n";
57 this.TestInfo.Text += "深度克隆数据:" + sameNamePerson.PersonName.LastName + "\r\n";
58 }
显示结果:
深度克隆原始数据:jill
深度克隆数据:clr_
浅表克隆原始数据:jill
浅表克隆数据:Shadow
深度克隆数据:clr_
特别说明的是:
DataSet
DataSet有个Clone和继承ICloneable后调用MemberwiseClone结果不一样,至于Copy属于浅表克隆还是深度克隆,暂时观察到结果。
还是先看下代码吧:
2{
3 DataSet first = new DataSet();
4 DataTable table = first.Tables.Add("table");
5 table.Columns.Add("C1", typeof(int));
6 table.Columns.Add("C2", typeof(char));
7 table.PrimaryKey = new DataColumn[] { table.Columns[0] };
8 for (int i = 0; i < 26; ++i)
9 {
10 table.Rows.Add(new object[] { i, (char)((ushort)'a' + i) });
11 }
12
13 DataSet clone = first.Clone();
14
15 DataSet copy = first.Copy();
16
17 first.Dispose();
18
19 if (first == null)
20 this.TestInfo.Text += "first has disponsed! \r\n";
21
22 this.TestInfo.Text += "Clone 结构,包括几张表:" + clone.Tables.Count.ToString() + "\r\n";
23 if (clone.Tables.Count > 0)
24 {
25 this.TestInfo.Text += "表列还在么?" + clone.Tables[0].Columns.Count.ToString() + "\r\n";
26 this.TestInfo.Text += "表数据还在么?行数:" + clone.Tables[0].Rows.Count.ToString() + "----我clone本身就没有数据\r\n";
27 }
28
29 this.TestInfo.Text += "Copy 结构,包括几张表:" + copy.Tables.Count.ToString() + "\r\n";
30 if (clone.Tables.Count > 0)
31 {
32 this.TestInfo.Text += "表列还在么?列数:" + copy.Tables[0].Columns.Count.ToString() + "\r\n";
33 this.TestInfo.Text += "表数据还在么?行数:" + copy.Tables[0].Rows.Count.ToString() + "\r\n";
34 }
35
36}
37
38
测试结果:
Clone 结构,包括几张表:1
表列还在么?2
表数据还在么?行数:0----我clone本身就没有数据
Copy 结构,包括几张表:1
表列还在么?列数:2
表数据还在么?行数:26
为了让浏览者理解原始的,我拷贝了MSDN的部分资料:
[Begin]
.NET on Clonation
You find two kinds of types in .NET: value and reference. The former includes primitive types, enums and structs, and is allocated on the stack. The latter includes classes and arrays and goes to the heap. Basically, a value type carries all of its content with it. By contrast, a reference type points to a memory buffer where all, or part, of its content is maintained.
In .NET you can duplicate objects in two ways that offer different functionality and power. You can create either a shallow or deep copy of a .NET object.
A shallow copy is a copy of the object only. If the object has a complex structure and contains references to child objects, these won't be duplicated. Any of these child objects will refer to the original object in the shallow copy. (More or less what happens with the data, but not with bookmarks and filters, in a cloned ADO Recordsets.)
A deep copy is a full copy of the object. You get a new object in which everything that is directly or indirectly referenced by the original object is duplicated.
The root object of the .NET Framework, namely the Object object, has a method called MemberwiseClone that creates a shallow copy of the current object.
protected object MemberwiseClone();
This method represents the built-in capability of .NET objects to implement shallow copy. The method is protected and cannot be overridden.
If you think that the standard shallow copy mechanism doesn't work for your objects, you either provide a brand new method to clone/copy them or you implement the ICloneable interface. If you want more than the standard clonation—a shallow copy as described earlier—some measure must be taken.
[End]
无论生活、还是技术,一切都不断的学习和更新~~~努力~