What?s the difference between the System.Array.CopyTo() and
System.Array.Clone()?
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
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 |
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 |
What is int tryparse in c#?
What is cli in c#?
How will you allow a class to be inherited, but prevent the method from being over-ridden?
What is the syntax for calling an overloaded constructor within a constructor?
Explain the Abstract class in c#.net
What is the file extension for c#?
Can we create instance for Abstract class?
28 Answers HCL, Satyam, SoftSol, Wipro,
What is the importance of closing an ado.net application?
can we create an empty interface with no definitions? If so, how it should be called in the class?
What are the steps for creating clr trigger
What is .dbml file?
How can I create a process that is running a supplied native executable (e.g., Cmd.exe)?