Sobes.tech
Middle

What is the difference between the Clone and CopyTo methods for arrays?

sobes.tech AI

Answer from AI

Clone creates a shallow copy of the array, returning a new instance of type Array with the same elements. CopyTo copies the elements of the current array into another existing array, starting at the specified index. Clone returns an object, requiring casting, whereas CopyTo is void and does not return a value.

// Example of using Clone
int[] originalArray = { 1, 2, 3 };
int[] clonedArray = (int[])originalArray.Clone(); // Creates a new copy

// Example of using CopyTo
int[] destinationArray = new int[5];
originalArray.CopyTo(destinationArray, 1); // Copies elements into destinationArray starting at index 1
What is the difference between the Clone and CopyTo… - sobes.tech