System.Array.CopyTo() 和 System.Array.Clone() 的区别 [英] Difference between the System.Array.CopyTo() and System.Array.Clone()
问题描述
System.Array.CopyTo()
和 System.Array.Clone()
有什么区别?
推荐答案
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.
删除错误的例子.
这篇关于System.Array.CopyTo() 和 System.Array.Clone() 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!