8
Reply

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

Sapna Malik

Sapna Malik

15y
21.6k
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.

    Please refer to the following URL to know the differences between System.Array.CopyTo() and System.Array.Clone(),http://onlydifferencefaqs.blogspot.in/2012/08/difference-between-clone-and-copyto.html

    String is immutable in nature i.e. every time you need to make a change to the string a new object will be created and then the change will be done and reflected.

    When there is a requirement of frequent repeated changes in string STRING BUILDER is used instead of string.

    However, string is more powerful in terms of methods than the string builder.

    System.Array.copyto() will perform a deep copy of array while system.array.Clone(0 will perform shalow copy of the array.Shalow copy will simply copy the elements of the array where as an Deep copy will copy the elements as well as any referenced elements present in the array.

    Array.CopyTo() copies all contents of original array to another array by specied index of original array while Clone creates a shallow copy. Array.CopyTo() copies Data as well as structure while Clone copies data only.

    Still need more detailed info refer below link

    http://www.csharpfriends.com/Forums/ShowPost.aspx?PostID=28509

    System.Array.CopyTo copies all the contents of the existing array while System.Array.Clone creates a shallow model similar to the existing array.

    The System.Arry.Clone() method returns a new array (a shallow copy) object containing all the elements in the original array. The System.Arry.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.