Difference between the System.Array.CopyTo() and System.Array.Clone() ? | C# interview Questions 31

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

System.Array.CopyTo():

Purpose: Copies the elements of an array to another existing array starting at a specified index.
Operation: Requires a destination array with sufficient size.
Usage: sourceArray.CopyTo(destinationArray, startIndex);
System.Array.Clone():

Purpose: Creates a shallow copy of the array.
Operation: Returns a new array with the same elements.
Usage: Array newArray = (Array)sourceArray.Clone();
Key Difference: CopyTo() copies elements to an existing array, while Clone() creates a new array with the same elements.

Leave a Reply

Your email address will not be published. Required fields are marked *