Differences between Array.CopyTo() and Array.Clone() in C#
An array is a contiguous memory allocation. It is used to store data collection of the same data types. Most of the time, we need to copy one array to another to perform computations without altering the original array. There are various ways to execute the copy operation, but in this Answer, we will compare Array.CopyTo() and Array.Clone() methods.
The Array.CopyTo( ) method
The CopyTo method of the array class copies all the data in the array to an existing array. It performs a
Syntax
The syntax of the CopyTo method of the Array class is:
Array.CopyTo(destArr, index);
Arrayis the array we want to copy the contents.destArris the array of the same data type asArraywhere the content will be copied to.indexis the integer from where we want to start copying ondestArr.
Example
The following is an example of copying array data to a new array using the CopyTo() method.
class Educative{static void Main(){var arr1 = new[] { "Lodhi", "Educative", "Faheem", "Welcomes","You" };var arr2= new string[10];arr2[0]="Ed Tech";// cloning arr and storing it in new arrayarr1.CopyTo(arr2,1);// Printing array using loopforeach (var element in arr2){System.Console.WriteLine(element);}}}
Explanation
- Line 5: We initialize an array
arr1of size 5. - Lines 6–7: We initialize an array
arr2with a memory allocation of 10 indexes and assign the first index with a string "Ed Tech." - Line 9: We call the
copyto()method ofarr1copying its elements toarr2starting with index 1. - Lines 11–14: We print the elements of the array
arr2.
The Array.Clone( ) method
The Clone() method of the Array class copies all the data in the array and returns a new object. It needs to be typecast to the datatype of the original array. It performs a
Syntax
The following is the syntax of the Clone() method in C#:
(datatype[])array.Clone()
datatype[]is the data type of the originalarray. It needs to be .typecast The conversion of one datatype to another. arrayis the array that we want to copy.
Note: If you want to learn more about
Array.Clone()method, check this answer.
Example
An example of copying an array by using the Clone() method is the following:
class Educative{static void Main(){var arr = new[] { "Lodhi", "Educative", "Faheem", "Welcomes","You" };// cloning arr and storing it in new arrayvar new_arr = (string[])arr.Clone();// Printing array using loopforeach (var element in new_arr){System.Console.WriteLine(element);}}}
Major Differences
Array.Copyto( ) | Array.Clone( ) |
Copyto( ) copies the source array to desitination array. | Clone() copies the array and returns the new object. |
It takes two parameters, that is, the destination array and index. | It takes a single parameter. |
It starts copying from the given index of the new array to the end of the array. | It always copies all content of the array. |
It is slightly slower because it is a wrapper of the copy method. | It is slightly faster. |
It does not need typecasting. | It needs to typecast the result to the original array datatype. |
Free Resources