Difference between the System.Array.CopyTo() and System.Array.Clone()

前端 未结 13 2093
攒了一身酷
攒了一身酷 2020-12-02 06:50

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

相关标签:
13条回答
  • 2020-12-02 07:18

    Clone() is used to copy only structure of data/array it doesn't copy the actual data.

    CopyTo() copies the structure as well as actual data.

    0 讨论(0)
  • 2020-12-02 07:21
    object[] myarray = new object[] { "one", 2, "three", 4, "really big number", 2324573984927361 };
    
    //create shallow copy by CopyTo
    //You have to instantiate your new array first
    object[] myarray2 = new object[myarray.Length];
    //but then you can specify how many members of original array you would like to copy 
    myarray.CopyTo(myarray2, 0);
    
    //create shallow copy by Clone
    object[] myarray1;
    //here you don't need to instantiate array, 
    //but all elements of the original array will be copied
    myarray1 = myarray.Clone() as object[];
    
    //if not sure that we create a shalow copy lets test it
    myarray[0] = 0;
    Console.WriteLine(myarray[0]);// print 0
    Console.WriteLine(myarray1[0]);//print "one"
    Console.WriteLine(myarray2[0]);//print "one"
    

    the source

    0 讨论(0)
  • 2020-12-02 07:22

    Please note: There is a difference between using String[] to StringBuilder[].

    In String - if you change the String, the other arrays we have copied (by CopyTo or Clone) that points to the same string will not change, but the original String array will point to a new String, however, if we use a StringBuilder in an array, the String pointer will not change, therefore, it will affect all the copies we have made for this array. For instance:

    public void test()
    {
        StringBuilder[] sArrOr = new StringBuilder[1];
        sArrOr[0] = new StringBuilder();
        sArrOr[0].Append("hello");
        StringBuilder[] sArrClone = (StringBuilder[])sArrOr.Clone();
        StringBuilder[] sArrCopyTo = new StringBuilder[1];
        sArrOr.CopyTo(sArrCopyTo,0);
        sArrOr[0].Append(" world");
    
        Console.WriteLine(sArrOr[0] + " " + sArrClone[0] + " " + sArrCopyTo[0]);
        //Outputs: hello world hello world hello world
    
        //Same result in int[] as using String[]
        int[] iArrOr = new int[2];
        iArrOr[0] = 0;
        iArrOr[1] = 1;
        int[] iArrCopyTo = new int[2];
        iArrOr.CopyTo(iArrCopyTo,0);
        int[] iArrClone = (int[])iArrOr.Clone();
        iArrOr[0]++;
        Console.WriteLine(iArrOr[0] + " " + iArrClone[0] + " " + iArrCopyTo[0]);
       // Output: 1 0 0
    }
    
    0 讨论(0)
  • 2020-12-02 07:23

    The Clone() method don't give reference to the target instance just give you a copy. the CopyTo() method copies the elements into an existing instance.

    Both don't give the reference of the target instance and as many members says they give shallow copy (illusion copy) without reference this is the key.

    0 讨论(0)
  • 2020-12-02 07:24

    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.

    0 讨论(0)
  • 2020-12-02 07:27

    Array.Clone doesn't require a target/destination array to be available when calling the function, whereas Array.CopyTo requires a destination array and an index.

    0 讨论(0)
提交回复
热议问题