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

How do you specify a custom attribute for the entire assembly (rather than for a class)?

726


What is an Interface in C#?

731


what are the Disadvantages of vb

785


What happens if you add duplicate elements to a set?

682


What are All kind of access specifiers for a class and for methods

812


What is a partial class in c#?

666


Explain the steps to create satellite assembly?

666


What is difference between private, protected, and public in C#?

776


What is global namespace in c#?

696


Explain acid rule of thumb for transactions in c#.

706


What is null in database?

644


What are the classes contained in a single .NET DLL ?

691


When was .net linq added?

719


Can abstract classes be final?

704


What is the difference between null and string empty in c#?

637