所述System.Array.CopyTo()和System.Array.Clone之间差() [英] Difference between the System.Array.CopyTo() and System.Array.Clone()
问题描述
有什么之间的差异 System.Array.CopyTo()
和 System.Array.Clone()
?
What’s the difference between the System.Array.CopyTo()
and System.Array.Clone()
?
推荐答案
在的克隆() 方法返回一个新数组包含原始数组中的所有元素(浅表副本)对象。在 CopyTo从() 方式复制元素融入另一个现有阵列。这两个执行浅表副本。浅表副本意味着内容(每个数组元素)包含对同一个对象与原始数组中的元素。深拷贝(而这些方法都不执行)将创建每个元素的对象的新实例,产生了不同,但相同的对象。
The Clone() method returns a new array (a shallow copy) object containing all the elements in the original array. The CopyTo() method copies the elements into another existing array. Both perform a shallow copy. A shallow copy means the contents (each array element) contains references to the same object as the elements in the original array. A deep copy (which neither of these methods performs) would create a new instance of each element's object, resulting in a different, yet identical object.
所以,区别是:
1- CopyTo require to have a destination array when Clone return a new array.
2- CopyTo let you specify an index (if required) to the destination array.
编辑:
删除了错误的例子。
Remove the wrong example.
这篇关于所述System.Array.CopyTo()和System.Array.Clone之间差()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!