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

Category: C SharpWhat’s the difference between the Array.CopyTo() and Array.Clone()?
Editor">Editor Staff asked 2 years ago

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

1 Answers
Editor">Editor Staff answered 2 years ago

The Array.Clone() method creates a shallow copy of an array. A shallow copy of an Array copies only the elements of the Array, whether they are reference types or value types, but it does not copy the objects that the references refer to. The references in the new Array point to the same objects that the references in the original Array point to.

 
The CopyTo() static method of the Array class copies a section of an array to another array. The CopyTo method copies all the elements of an array to another one-dimension array. The code listed in Listing 9 copies contents of an integer array to an array of object types.