Difference between the System.Array.CopyTo() and System.Array.Clone()

前端 未结 13 2094
攒了一身酷
攒了一身酷 2020-12-02 06:50

What’s the difference between the System.Array.CopyTo() and System.Array.Clone()?

相关标签:
13条回答
  • 2020-12-02 07:41

    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.

    So the difference are :

    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.
    
    Edit:

    Remove the wrong example.

    0 讨论(0)
提交回复
热议问题