By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,669 Members | 2,203 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,669 IT Pros & Developers. It's quick & easy.

Shalow copy vs Deep copy

P: n/a
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/...4/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/...99377207834383)
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."

Nov 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
Nov 17 '05 #2

P: n/a
fperfect13 <fe****@gmail.com> wrote:

<snip>
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).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3

P: n/a
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. :)

Nov 17 '05 #4

P: n/a
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

Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.