Ron_B
Ron_B

Reputation: 66

System.Array.CopyTo() Issue

I have researched this issue and cannot seem to find the any worthwhile answer. What is the difference between System.Array.CopyTo() and System.Array.Clone()?

Upvotes: 0

Views: 234

Answers (2)

Byron Lo
Byron Lo

Reputation: 464

The CopyTo method will allow you to append to another array.

for example, if you have an array of size 100 and another array of size 200, you can use the CopyTo method to Copy the array of size 100 to the last one hundred slots of the larger array. Or even copy it to the larger array starting at position 50 etc.

Clone will simply create an identical (shallow copy) of your existing array.

Upvotes: 2

Tilak
Tilak

Reputation: 30728

System.Array.CopyTo will copy to existing array of the similar size(If size of destination is less than size of source data, exception will be thrown). System.Array.Clone will create a new array.

From MSDN: Array.CopyTo : Copies all the elements of the current one-dimensional Array to the specified one-dimensional Array starting at the specified destination Array index. The index is specified as a 32-bit integer.

Array.Clone: Creates a shallow copy of the Array.

Upvotes: 2

Related Questions