Shalow copy vs Deep copy

F

fperfect13

Hi,

I wanted to perform a deep copy of an array. Searching on google I ran
into different opinions :

C# Interview Questions
(http://blogs.wwwcoder.com/tsvmadhav/archive/2005/04/08/2882.aspx)

10.What's the difference between the System.Array.CopyTo() and
System.Array.Clone()?
The first one performs a deep copy of the array, the second one is
shallow. A shallow copy of an Array copies only the elements of the
Array, whether they are reference types or value types, but it does not
copy the objects that the references refer to. The references in the
new Array point to the same objects that the references in the original
Array point to. In contrast, a deep copy of an Array copies the
elements and everything directly or indirectly referenced by the
elements.

http://blogs.crsw.com/mark/articles/252.aspx
10.What's the difference between the System.Array.CopyTo() and
System.Array.Clone()?
The Clone() method returns a new array (a shallow copy) object
containing all the elements in the original array. The CopyTo() method
copies the elements into another existing array. Both perform a
shallow copy. A shallow copy means the contents (each array element)
contains references to the same object as the elements in the original
array. A deep copy (which neither of these methods performs) would
create a new instance of each element's object, resulting in a
different, yet identacle object.

(http://groups.msn.com/PuneUserGroup...Message=2957&LastModified=4675499377207834383)
27. What's the difference between the System.Array.CopyTo() and
System.Array.Clone()?
The first one performs a deep copy of the array, the second one is
shallow.

So I quote:
"The first one performs a deep copy of the array" ,
"Both perform a shallow copy."

I used System.Array.CopyTo() and it seems to perform only a shallow
copy, so how can I perform a deep one (one method would be to
serialize/deserialize the object) and the second quetion: Is CopyTo()
making a deep copy or not?

MSDN:"In contrast, a deep copy of an Array copies the elements and
everything directly or indirectly referenced by the elements."
 
C

Cor Ligthert

FPerfect.

A copyto makes a copy of the values in/from a one dimensional array (where
the type of the sender and the receiver has to be the same and the receiver
completly exist already).

A clone copies the references, therefore using the references in the clone
will have the same effect on the referenced objects as using the original
array. Although you can have at a certain moment in a clone more references
than in the original or/and visa versa.

A deepcopy you have to make yourself. The system does not know what objects
you have added to by instance your arraylist. (It can be by instance a
thousand times deep arraylist).

I hope that this short explanation gives an idea?

Cor
 
J

Jon Skeet [C# MVP]

So I quote:
"The first one performs a deep copy of the array" ,
"Both perform a shallow copy."

I used System.Array.CopyTo() and it seems to perform only a shallow
copy, so how can I perform a deep one (one method would be to
serialize/deserialize the object) and the second quetion: Is CopyTo()
making a deep copy or not?

No, CopyTo isn't making a deep copy. To make a deep copy, you basically
have to go through and do whatever you want to do to make a deep copy
of each element (whether that's to use serialization, implement your
own IDeepCopyable interface or whatever).
 
B

Bruce Wood

There is no deep copy operation; you would have to implement it
yourself.

By the way, the first list of Interview Questions on C# contains other
errors as well. Item #4 says:

4. Describe the accessibility modifier "protected internal".
It is available to classes that are within the same assembly and
derived from the specified base class.

This is inaccurate. "protected internal" in C# makes a construct
available to classes that are in the same assembly _or_ classes derived
from the declaring class. The answer gives the impression that
"protected internal" is more restrictive than protected and more
restrictive than internal. In fact it's less restrictive than either
one.

So... I wouldn't take Q & A lists like this as accurate... unless
they're written by the C# team, that is. :)
 
F

fperfect13

Thanks, i'm clear now with deep copy but I encountered a problem with
deep copy throught serialization but I will post it in another thread
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top