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 / asit pal

I felt the need of this posting because I have seen postings
(not only in this forum but also in many others) saying
that "CopyTo makes a deep copy and Clone makes a shallow
copy." This is absolutely wrong.

Both CopyTo() and Clone() make shallow copy. Clone() method
makes a clone of the original array. It returns an exact
length array.

On the other hand, CopyTo() copies the elements from the
original array to the destination array starting at the
specified destination array index. Note that, this adds
elements to an already existing array.

The following code will contradict the postings saying that
CopyTo makes a deep copy:

public class Test
{
public string s;
}

...

private void test()
{
Test[] array = new Test[1];
array[0] = new Test();
array[0].s = "ORIGINAL";

Test[] copy = new Test[1];
array.CopyTo(copy, 0);

// Next line displays "ORIGINAL"
MessageBox.Show("array[0].s = " + array[0].s);

copy[0].s = "CHANGED";

// Next line displays "CHANGED", showing that
// changing the copy also changes the original.
MessageBox.Show("array[0].s = " + array[0].s);
}

Let me explain it a bit. If the elements of the array are
of reference types, then the copy (both for Clone() and
CopyTo()) will be made upto the first(top) level. But the
lower level doesn't get copied. If we need copy of lower
level also, we have to do it explicitly. That's why after
Cloning or Copying of reference type elements, each element
in the Cloned or Copied array refers to the same memory
location as referred by the corresponding element in the
original array. This clearly indicates that no seperate
instance is created for lower level. And if it were so then
changing the value of any element in the Copied of Cloned
array would not have effect in the corresponding element of
the original array.

I think that my explanation is exhaustive but I found no
other way to make it understandable. Hope this will help
everyone.

Is This Answer Correct ?    23 Yes 2 No

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

Answer / kishore anumala

Clone creates the new array of same length of an existing
array and then copies the data.

Where as copyto copies the data from source to destination
array. Copyto does not creates any new array.

Is This Answer Correct ?    10 Yes 2 No

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

Answer / sree..

The below code does a deep copy.
Let me know if something is wrong with the code.

string[] array = new string[1] { "aaa" };
string[] copy = new string[1];
array.CopyTo(copy, 0);

copy[0] = "xyz";
Console.WriteLine(array[0]);

I see array[0] value as "aaa".

Is This Answer Correct ?    6 Yes 0 No

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

Answer / jiturcm

System.Array.CopyTo() performs deep copy and shallow.
Where as System.Array.clone() performs shallow copy.

System.Array.CopyTo() doesn't create new Array
System.Array.clone() create new Array of same length of existing array.

If System.Array.CopyTo()can perform both deep copy and
shallow then why System.Array.clone ?

Is This Answer Correct ?    3 Yes 2 No

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

Answer / gp_bellamkonda

Clone copies only data but not structure
array.copy copies structure as well as data

Is This Answer Correct ?    5 Yes 5 No

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

Answer / guest

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

Is This Answer Correct ?    9 Yes 17 No

Post New Answer

More C Sharp Interview Questions

Explain About web methods and its various attributes

0 Answers   Digital GlobalSoft,


What are partial classes and use of partial classes?

0 Answers   QuestPond,


Is list immutable in c#?

0 Answers  


Can we extend sealed class in c#?

0 Answers  


What will be the output of the following code?

0 Answers  






Where test director stores its data ? Database ,Local file etc...? I need to read this data from Visual Studio 2005 c# client. Regards

0 Answers  


Hi, Can we implement the Abstract class on interface in c#, If yes then provide the code implementation

1 Answers   RHR, Wipro,


What is a web service in c#?

0 Answers  


What is the major difference between a custom control and user control?

0 Answers   C DAC, CDAC,


What is the components of window?

0 Answers  


What is difference between arraylist and list in c#?

0 Answers  


How are delegates chosen?

0 Answers  


Categories