473,508 Members | 2,460 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Shalow copy vs Deep copy

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
4 52280
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
12340
by: Alex | last post by:
Entering the following in the Python shell yields >>> help(dict.copy) Help on method_descriptor: copy(...) D.copy() -> a shallow copy of D >>>
4
12433
by: Dennis | last post by:
I have several Data Structures, say "mystruct" which contain arrays of bytes, other structures, etc. I then dimension a variable (var1) as "mystruct" and set the various elements var1 to data. I...
6
4304
by: Desmond Cassidy | last post by:
Hi, I'm sure this has been asked several times before but I'll risk it ;-) If I wish to save an Arraylist to another Arraylist and work on te original without affecting the contents of the new...
1
3609
by: Miljana Murgas | last post by:
I have a struct like this: public struct Vec { public float m; public Vec(float v1, float v2) { m = new float; m=v1; m=v2; }
5
25629
by: BenW | last post by:
Hello, What is the easiest way to make "deep copy" of my Hashtable? How about with other Collection classes in C#, any documents available? I don'r actually understand why Framework's...
26
15765
by: saxenavaibhav17 | last post by:
what is Deep Copy, Shallow copy and Bitwise copy, Memberwise copy? and what is the difference between them? pls help vaibhav
22
15904
by: Steven Blair | last post by:
I need to perform a Deep Copy on an ArrayList. I wrote a small sample app to prove this could be done: ArrayList a = new ArrayList(); ArrayList b = new ArrayList(); a.Add("Hello"); b =...
4
3132
by: shuisheng | last post by:
Dear All, Is there any easy way to make sure all my object copies are deep copy or shallow copy? I do not like to implement it in each class one by one. Thanks, Shuisheng
3
3888
by: jacek.dziedzic | last post by:
Hello! Suppose I have a class base, with virtual methods and a virtual destructor and a bunch of classes, derived1, derived2, ... which publicly derive from base. I then have a pointer base*...
0
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7405
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5643
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3214
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.