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

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

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

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

    As stated in many other answers both methods perform shallow copies of the array. However there are differences and recommendations that have not been addressed yet and that are highlighted in the following lists.

    Characteristics of System.Array.Clone:

    • Tests, using .NET 4.0, show that it is slower than CopyTo probably because it uses Object.MemberwiseClone;
    • Requires casting the result to the appropriate type;
    • The resulting array has the same length as the source.

    Characteristics of System.Array.CopyTo:

    • Is faster than Clone when copying to array of same type;
    • It calls into Array.Copy inheriting is capabilities, being the most useful ones:
      • Can box value type elements into reference type elements, for example, copying an int[] array into an object[];
      • Can unbox reference type elements into value type elements, for example, copying a object[] array of boxed int into an int[];
      • Can perform widening conversions on value types, for example, copying a int[] into a long[].
      • Can downcast elements, for example, copying a Stream[] array into a MemoryStream[] (if any element in source array is not convertible to MemoryStream an exception is thrown).
    • Allows to copy the source to a target array that has a length greater than the source.

    Also note, these methods are made available to support ICloneable and ICollection, so if you are dealing with variables of array types you should not use Clone or CopyTo and instead use Array.Copy or Array.ConstrainedCopy. The constrained copy assures that if the copy operation cannot complete successful then the target array state is not corrupted.

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

    Both perform shallow copies as @PatrickDesjardins said (despite the many misled souls who think that CopyTo does a deep copy).

    However, CopyTo allows you to copy one array to a specified index in the destination array, giving it significantly more flexibility.

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

    One other difference not mentioned so far is that

    • with Clone() the destination array need not exist yet since a new one is created from scratch.
    • with CopyTo() not only does the destination array need to already exist, it needs to be large enough to hold all the elements in the source array from the index you specify as the destination.
    0 讨论(0)
  • 2020-12-02 07:32

    Both are shallow copies. CopyTo method is not a deep copy. Check the following code :

    public class TestClass1
    {
        public string a = "test1";
    }
    
    public static void ArrayCopyClone()
    {
        TestClass1 tc1 = new TestClass1();
        TestClass1 tc2 = new TestClass1();
    
        TestClass1[] arrtest1 = { tc1, tc2 };
        TestClass1[] arrtest2 = new TestClass1[arrtest1.Length];
        TestClass1[] arrtest3 = new TestClass1[arrtest1.Length];
    
        arrtest1.CopyTo(arrtest2, 0);
        arrtest3 = arrtest1.Clone() as TestClass1[];
    
        Console.WriteLine(arrtest1[0].a);
        Console.WriteLine(arrtest2[0].a);
        Console.WriteLine(arrtest3[0].a);
    
        arrtest1[0].a = "new";
    
        Console.WriteLine(arrtest1[0].a);
        Console.WriteLine(arrtest2[0].a);
        Console.WriteLine(arrtest3[0].a);
    }
    
    /* Output is 
    test1
    test1
    test1
    new
    new
    new */
    
    0 讨论(0)
  • 2020-12-02 07:38

    The answers are confusing to me. When you say shallow copy, this means that they are still pointing to the same address. Which means, changing either one will change another as well.

    So if I have A = [1,2,3,4] and I clone it and get B = [1,2,3,4]. Now, if I change B[0] = 9. This means that A will now be A = [9,2,3,4]. Is that correct?

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

    Array.Clone() would perform technically deep copy, when pass the array of int or string to a method as a reference.

    For example

    int[] numbers = new int[] { -11, 12, -42, 0, 1, 90, 68, 6, -9 }; 
    
    SortByAscending(numbers); // Sort the array in ascending order by clone the numbers array to local new array.
    SortByDescending(numbers); // Same as Ascending order Clone
    

    Even if the methods sort the array of numbers but it wont affect the actual reference passed to the sorting methods.i.e the number array will be in same unsorted initial format in line no 1.

    Note: The Clone should be done in the sorting methods.

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