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

How can you overload a method?

6 Answers  


Wcf and what is difference between wcf and web services?

0 Answers  


Which is faster array or arraylist in c#?

0 Answers  


What do you mean by saying a "struct is a value type"?

0 Answers  


please exaplain gridview and what are the process available for it. how to add the row number automatically? is it possible to add child controls ?

2 Answers  






What does the keyword virtual mean in the method definition?

1 Answers  


What are methods in C#?

0 Answers   Winsol Solutions,


Can a private virtual method can be overridden?

0 Answers  


Explain the difference between a sub and a function?

1 Answers   Wipro,


Why do we use parameters in c#?

0 Answers  


Can we inherit two classes in c#?

0 Answers  


Can abstract class have parameterized constructor?

0 Answers  


Categories