7
Reply

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

17y
22.3k
0
Reply

    Array.Clone() method makes a clone of the original array. It returns an exact length array. Array.Clone() method does not require the destination array to be existed as it creates a new one from scratch Array.CopyTo() copies the elements from the original array to the destination array starting at the specified destination array index. Note that, this adds elements to an already existing array. Array.CopyTo() require a destination array to be existed before and it must be capable to hold all the elements in the source array from the index that is specified to copy from the source array.

    To add more information

     

    Clone: This method does member wise clone all members except static members., internally uses member wise clone method of the object. Which is nothing but Prototype Pattern. when we say all members, it is val and ref types. When ref type members exists, it will never copy the referance obect, but after copy both of them points to the same source. If source changes all the object refering to the source will get changed. Which is also called as Shallow

    Deep Copy: Which copies the source objects as well. So that there will be two different copies of the sources and objects.

    Difference between the System.Array.CopyTo() and System.Array.Clone()
    System.Array.CopyTo():- System.Array.CopyTo() copies both structure and data
    System.Array.Clone():- it only copies structure, does not copy data.

    More important .NET Questions And Answers

    I felt the need of this posting because I have seen postings (not only in this forum but also in many others) saying that "CopyTo() makes a deep copy and Clone() makes a shallow copy."  This is absolutely wrong. 

    Both CopyTo() and Clone() make shallow copy. Clone() method makes a clone of the original array. It returns an exact length array.

     On the other hand, CopyTo() copies the elements from the original array to the destination array starting at the specified destination array index. Note that, this adds elements to an already existing array.

    The following code will contradict the postings saying that CopyTo() makes a deep copy:

    public class Test
    {
        public string s;

    // Write Main() method and within it call test() 

    private void test()
    {
        Test[] array = new Test[1];
        array[0] = new Test();
        array[0].s = "ORIGINAL";

        Test[] copy = new Test[1];
        array.CopyTo(copy, 0);

        // Next line displays "ORIGINAL"
        MessageBox.Show("array[0].s = " + array[0].s);
        copy[0].s = "CHANGED";

        // Next line displays "CHANGED", showing that
        // changing the copy also changes the original.
        MessageBox.Show("array[0].s = " + array[0].s);
    }

    Let me explain it a bit. If the elements of the array are of reference types, then the copy (both for Clone() and CopyTo()) will be made upto the first(top) level. But the lower level doesn't get copied. If we need copy of lower level also, we have to do it explicitly. That's why after Cloning or Copying of reference type elements, each element in the Cloned or Copied array refers to the same memory location as referred by the corresponding element in the original array. This clearly indicates that no separate instance is created for lower level. And if it were so then changing the value of any element in the Copied or Cloned array would not have effect in the corresponding element of the original array.

    I think that my explanation is exhaustive but I found no other way to make it understandable. Hope this will help everyone.

    I think the response below is kind of a distinction w/o a difference. If the array copied to has the same number of elements as the one copied, both structures and data are the same. I would say the difference is that clone creates a new array object; CopyTo requires a separate existing array object.

    System.Array.CopyTo() - it copy the structure of an array not data System.Array.Clone() - it copy the structure of an array along with the information(data)

    hi.......

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

    plz reply.......

    17y
    0