This is a discussion on CopyTo and Clone within the C# Programming forums, part of the Software Development category; In C#; between the System.Array.CopyTo() and System.Array.Clone(), what are their differences? Where can you use copyTo ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| The first one performs a deep copy of the array, the second one is shallow. ![]() 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. A deep copy (which neither of these methods performs) would create a new instance of each element's object, resulting in a different, yet identacle object. Array.CopyTo() Copies all the elements of the current one-dimensional Array to the specified one-dimensional Array. Array.Clone Method Creates a shallow copy of the 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. thanks ![]()
__________________ Karpagarajan. R Necessity is the mother of invention Last edited by Karpagarajan : 04-06-2007 at 12:56 AM. |
| |||
| System.Array.CopyTo() method copies all the elements of the current one dimensional Array to the specified one dimensional Array. System.Array.Clone method creates a shallow copy of the Array. A shallow copy of Array copies only the element of the Array, whether they are referenced types or value types, but it does not copy the object 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. In contrast the deep copy of an Array copies the elements and everything directly or indirectly referenced by the elements.
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| Hi, I couldnt able to understand the difference between copyTo() and Clone(). Is there any sample coding which contains these two methods used in it
__________________ Krishnakumar.S Beware of Everything -that is un true; stick to the Truth shall succeed slowly but steadily |
| |||
| Hi Krishna kumar, CopyTo(destinationArray, Index) It copies the current instance of the array to the destination array. The copy will start from the Index specified in the argument. So, if Index is set to zero, it means, it copies all the elements of the source array to destination array. string[] arr=new string[2]; string[] arrTemp=new string[5]; arr[0] = "Sandip"; arr[1] = "Calcutta"; arr.CopyTo(arrTemp,2); This C# code snippet clones an entire array using Array.Clone. using System; class CloneArray { public static void Main() { string[] array1 = {"a", "b", "c"}; string[] array2 = (string[]) array1.Clone(); DumpArray ("Original array: ", array1); DumpArray ("Cloned array: ", array2); } private static void DumpArray (string title, string[] array) { Console.Write (title); foreach (string s in array) { Console.Write ("{0} ", s); } Console.WriteLine(); } } I hope this may help.... thnx.... |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Can anybody explain about the differences between dataset.clone and dataset.copy? | kingmaker | C# Programming | 2 | 08-28-2007 12:22 AM |
| What’s the difference between the System.Array.CopyTo() and System.Array.Clone() ? | Archer | C# Programming | 2 | 08-25-2007 03:00 AM |