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 do you inherit from a class in C#?

1 Answers  


Can you call from an inherited constructor to a specific base constructor if both base class and an inheriting class has a number of overloaded constructors?

0 Answers   Siebel,


How can I check the type of an object at runtime?

0 Answers  


what is difference between is and as operators in c#?

0 Answers   Winsol Solutions,


How to handle exceptions that are raised in a component?

0 Answers   Alcatel-Lucent,






What is a resource? Provide an example from your recent project.

2 Answers   IBM, Logica CMG, Wipro,


hi In my database i put id column as identity(the database is incremented by it self only)what i want is if i enter a data in my website, id will come automatically can u pl z tell me the code thax in advance

1 Answers  


What is the difference between ref & out parameters in c#?

0 Answers  


Is c# dictionary a hash table?

0 Answers  


What are examples of desktop applications?

0 Answers  


Is arraylist type safe in c#?

0 Answers  


What is keywords in c#?

0 Answers  


Categories