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

Answer Posted / sudhir sheoran

First of all both perform a shallow copy.
Difference is that:-

copy to() needs another array and index from which it starts copy elements of source array to destination array. E.g if destination array B[] has already 3 elements so we can pass 4 as index and B as destination array. This will make it more clear -

class CopyTO
{
public static void Main()
{
int[] A = new int[3];

int [] B = new int[7];
A[0] = 1;
A[1] = 2;
A[2] = 3;

B[0] = 4;
B[1] = 5;

A.CopyTo(B, 4);
for (int i = 0; i < B.Length; i++)
{
Console.WriteLine(B[i]);
}
}
}

The output will be 4,5,0,0,1,2,3 // It copied elements of A
from Index 4 in B.

Clone to return a array that contains elements of the source array. Its length is same as of source array.

Is This Answer Correct ?    2 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What does question mark mean in c#?

565


How do I automate my desktop application?

498


What is difference between dictionary and list in c#?

452


What is data quality assurance?

441


What is difference between static and constant variable?

527






What is data types in c#?

519


Is a structure a class?

496


What is .net console?

530


Where do we use serialization in c#?

496


Can abstract class instantiated c#?

494


What are the methods in c#?

525


What does int32 mean?

473


Is array reference type in c#?

484


What is a .exe extension files? How is it similar to .dll extension files?

574


When Should You Call The Garbage Collector In .net?

549