What?s the difference between the System.Array.CopyTo() and
System.Array.Clone()?
Answer Posted / 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 |
Post New Answer View All Answers
Can the accessibility of a type member be greater than the accessibility of its containing type?
Hi Friends, I am going through Siemens Interview Procedure from last 1+1/2 months. I went through 1 written + 2 Technical + 1 Managerial Round process after which I got call from HR informing that "you are selected and we would like to meet you for HR round". HR round was very nominal compared to MR. HR Round last for hardly 5 mins. They told me that you will get the final result on Friday. Still I have not received any feedback from them. Please help!!!
What is the use of 0 in c#?
What is asp net c#?
Where’s global assembly cache located on the system?
Explain namespaces in c#.
What are the new features in c# 2.0?
To allow an element to be accessed using a unique key which .NET collection class is used ?
What is a base class in C#?
What is a console file?
In gridview in editmode if we want to display information in one combobox based on
How will you get the different language strings?
What is the difference between finally and finalize block?
Can an abstract class inherit from another abstract class c#?
What are the different types of constructors in c#?