克隆和复制方法之间的区别 [英] Difference between Clone and Copy method

查看:159
本文介绍了克隆和复制方法之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Copy和Clone方法都创建了一个与原始DataTable具有相同结构的新DataTable。

Clone方法无法创建数据行,但Copy方法同时创建结构和数据流。我们为什么要使用Clone方法而不是Copy方法。请解释一下。非常感谢帮助我。

Both the Copy and the Clone methods create a new DataTable with the same structure as the original DataTable.
Clone method can not create datarow but Copy method create both structure and datarow. Why should we use Clone method instead of Copy method. please explain me. Advance thanks for helping me.

推荐答案

让我们看看:



Let's see:

class A {
   internal A A; // non-private fields is the bad coding style, but this is just for example
   internal Name;
} //A

A a = new A();
A b = a; // a and b reference the same object:
// if you change a.Name, b.Name will change

b = a.MemberwiseClone();
// not a and b are distinctly different objects
// they can have different names:
a.Name = "This is a";
b.Name = "This is b"; // different, you can check it up after this statement.

// Now, how about a.A?
// After cloning, it still references the same object, because A is a reference type:
A a = new A();
a.A = new A();
a.A.Name = "inner";

b = a.MemberwiseClone();
b.A.Name = "another inner";
// it will change a.A.Name! Why?
// because we cloned a, but not a.A





现在,查看上面显示的代码示例中的最后一条评论。这意味着克隆不是

参见:

http://msdn.microsoft.com/en-us/library/system.object.memberwiseclone.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.icloneable.aspx [ ^ ],

http://en.wikipedia.org/wiki/Clone_%28computing%29 [ ^ ]。



最后,请查看我最近关于深度克隆的答案:如何为列表类实现ICloneable? [ ^ ]。



-SA



Now, look at the last comment in the code sample shown above. It means that cloning is not deep.
See also:
http://msdn.microsoft.com/en-us/library/system.object.memberwiseclone.aspx[^],
http://msdn.microsoft.com/en-us/library/system.icloneable.aspx[^],
http://en.wikipedia.org/wiki/Clone_%28computing%29[^].

Finally, please see my recent answers on deep cloning: How to implement ICloneable for a List Class?[^].

—SA


区别非常直接。



DataTable.Clone 方法克隆dataTable的结构,包括所有dataTable模式和约束。 不是数据。



DataTable.Copy 方法复制结构和数据。



问候..
The difference is very straight forward.

DataTable.Clone method clones the structure of the dataTable, including all dataTable schemas and constraints. Not data.

DataTable.Copy method copies both the structure and data.

Regards..


访问这里了解更多...



http://www.dotnetfunda.com/forums/ thread7557-difference-between-dataset-clone-dataset-copy.aspx [ ^ ]
visit here to understand more...

http://www.dotnetfunda.com/forums/thread7557-difference-between-dataset-clone-dataset-copy.aspx[^]


这篇关于克隆和复制方法之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆