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

Answers were Sorted based on User's Feedback



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

Answer / 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

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

Answer / satish

System.Array.CopyTo() performs the deep copy that is it
creates a new duplicate copy without effect of old one.
Whereas Clone creates as shallow copy but as it is a clone
so it is linked with the old one so affect whenever changes
occures in the old one.

Is This Answer Correct ?    1 Yes 2 No

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

Answer / rahul

The first one performs a deep copy of the array, the second
one is shallow.

Is This Answer Correct ?    0 Yes 3 No

Post New Answer

More C Sharp Interview Questions

What is yield return in c#?

0 Answers  


What is view model in c#?

0 Answers  


What is difference between const and static in c#?

0 Answers  


What is the difference between static class and singleton class in c#?

0 Answers  


Explain when should you call the garbage collector in .net?

0 Answers  


how can i display crystalreport in button click? am working with VS2005

1 Answers  


What are the drawbacks of extending an interface as opposed to extending a class?

0 Answers  


What is unrecognized escape sequence in c#?

0 Answers  


What is the main method?

0 Answers  


Why array is faster than arraylist in c#?

0 Answers  


What are the types of serialization?

0 Answers  


What is the difference between a private assembly and a shared assembly?

0 Answers  


Categories