Hi
What is the difference between the System.Array.CopyTo() and System.Array.Clone() ?
Loading
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
VulpesRank #55 · 29.2K reputationPosted Jun 27, 2012, 8:04 AM EDT
Jignesh TrivediRank #16 · 50.3K reputationPosted Jun 27, 2012, 7:38 PM EDT
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.
http://msdn.microsoft.com/en-us/library/system.array.clone.aspx
http://msdn.microsoft.com/en-us/library/system.array.copyto.aspx
int[] Originalnumbers = { 1, 2, 3, 4, 5 };
int[] numbersClone = (int[])Originalnumbers.Clone();
numbersClone[2] = 0;
Console.WriteLine("Clone Example");
Console.WriteLine("----------------------");
Console.WriteLine(Originalnumbers[2]);//Display 4
Console.WriteLine(numbersClone[2]);//Display 0 !!! Not the same :)
Console.WriteLine("----------------------");
int[] numbersCopy = new int[6];
Originalnumbers.CopyTo(numbersCopy, 0);
numbersCopy[3] = 0;
Console.WriteLine("Copy Example");
Console.WriteLine("----------------------");
Console.WriteLine(Originalnumbers[3]);//Display 4
Console.WriteLine(numbersCopy[3]);//Display 0 !!! Not the same :)
Console.WriteLine("----------------------");
hope this will help you.