Article Options
Recently Viewed
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Framework  »  Copying vs. Cloning  »  Investigation
Copying vs. Cloning
by Phil Eakins | Published  02/09/2006 | .NET Framework | Rating:
Investigation

Introduction

In order to investigate the various methods of obtaining a copy of an array, I wrote a short program (the VS 2003 source code can be downloaded from the link) and systematically worked my way through copying or cloning both System and User Designed Types. 

These tests also introduce the concept of a ‘deep copy’, which means that a copy of any Object Type must be a completely independent copy of that Object. Everything directly or indirectly referenced by any field in that Object must also be copied.

A true deep copy was thought, by me at least, to be difficult to attain as it would have to be implemented on an object-by object basis, and only then so long as each object’s Type implemented the IClonable interface. However, Richter & Balena suggested a remedy using Streams. Balena enhanced the method in "Programming Microsoft Visual Basic .NET Version 2003" (MS Press 2004, ISBN 0-7356-2059-8) and is substantially used in the following tests as 'CloneObject'. The catch, of course, is that the objects in the Array being deep copied by this technique must each support Serialisation, as must each Object referred to by them.

Preparation

The following tables list the tests that were carried out on arrays of either Type in order to establish the effectiveness of producing a truly independent copy of that array.

The test of System Types is implemented by creating Array1() and filling it with appropriate data.  Array1() is then copied to Array2() using the selected method.

Array1() is altered using the method shown in Table 1 and the arrays are then compared for independence.

A list of the data used are shown in Appendix 1.

The Person Class, which supports both Clone and CloneObject methods, was subclassed into PetOwner and Team Classes.

The PetOwner Object can have a reference to a Pet Object, which does not have a Clone Method, or a SecondPet Object, which does.

Team Objects have no references.

Array1() is filled with one of the Person Objects, see Appendix 1.  Array1() is copied to Array2() using the chosen method, and then altered by changing Person, Pet or Team data as detailed in Appendix 1.  The arrays are then compared.

 

Comments    Submit Comment

Comment #1  (Posted by an unknown user on 02/09/2006)
Rating
Fantastic in-depth coverage of a topic that would otherwise frustrate people when they ran in to such problems.

Thanks!
 
Comment #2  (Posted by an unknown user on 02/28/2006)
Rating
It is Good
 
Comment #3  (Posted by an unknown user on 03/23/2006)
Rating
really excellent
 
Comment #4  (Posted by Rob Mrowiec on 06/20/2006)
Rating
This was a very good explanation of the topic of Cloning and what should be considered when deciding to copy or clone. Now if only someone would just remove all the weeds in the search engines so that it is easier to find this article!
 
Comment #5  (Posted by an unknown user on 07/23/2006)
Rating
Thank you for your in depth knowledge
Very helpful!
 
Comment #6  (Posted by an unknown user on 10/30/2006)
Rating
this definitely a very nice detailed explanation
 
Comment #7  (Posted by an unknown user on 11/03/2006)
Rating
great
 
Comment #8  (Posted by an unknown user on 12/01/2008)
Rating
orsitac
 
Sponsored Links