Difference between the System.Array.CopyTo() and System.Array.Clone in DotNet Framework
Loading
Difference between the System.Array.CopyTo() and System.Array.Clone in DotNet Framework
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Lokendra SinghRank #279 · 8.8K reputationPosted Jun 7, 2025, 7:44 AM EDT
Here's a comparison between
System.Array.CopyTo()andSystem.Array.Clone()in .NET Framework:sourceArray.CopyTo(destArray, startIndex)(int[])sourceArray.Clone()Example:
int[] source = {1, 2, 3, 4, 5}; int[] destination = new int[10]; source.CopyTo(destination, 2); // Copies to index 2-6 int[] source = {1, 2, 3, 4, 5}; int[] cloned = (int[])source.Clone(); // Creates new array [1,2,3,4,5]Both methods perform shallow copying, meaning for reference types, only references are copied, not the actual objects they point to.
Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Jun 9, 2025, 6:57 AM EDT
Array.Clone()Clone()creates a new array, so changes tocloneddon't affectoriginal.int[] original = { 1, 2, 3 }; int[] cloned = (int[])original.Clone(); cloned[0] = 100; Console.WriteLine(original[0]); // Output: 1 Console.WriteLine(cloned[0]); // Output: 100Array.CopyTo()CopyTo()requires an existing array to copy into.int[] original = { 1, 2, 3 }; int[] target = new int[3]; original.CopyTo(target, 0); target[0] = 100; Console.WriteLine(original[0]); // Output: 1 Console.WriteLine(target[0]); // Output: 100Good Luck!