Article Options
Premium Sponsor
Premium Sponsor

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

The results of the test are tabulated below:

Testing for Existance of Second Array

There is a facility to compare pointers to see whether they point to the same Object on the Managed Heap,  Array1() is Array2().  

I have used this comparison to provide the text for the ‘Independence’ Label on the lower right corner of the Form in the accompanying program. The test takes place after the data is written for the second time.

Whilst it provides accurate results for the System Types it cannot serve for the User Designed Types as (although the Pointers might not be to the same Object) the content of those Arrays is, in some cases, not the required independent copy (see Table 4 with Table 5).

Conclusion

This has been a most interesting exercise and in doing the research I have learned some valuable lessons about how .NET 1.1 organises variables in memory. In particular, I now know what to avoid!

As can be easily seen, the copying of an array of System Types if straightforward, but making a straight copy of the Array pointer to another variable should be avoided, unless a ‘second’ copy is required of course.

User Designed Types are also straightforward so long as there is no type referred to within the Object, a little more complicated if there is, and impossible to do by a Native Framework Method if the Object referred to does not, itself, support cloning.

However, one thing that stands out very clearly indeed is that any copy involving the CloneObject Method/Function will succeed no matter what, provided that all of the objects involved are serialisable.

All of the tests were carried out using the associated program (developed using VS2003) which can be downloaded, with the source code, from the link.  Please feel free to explore different options and to change the code as you see fit.  I should, however, like to hear of your experiences so that I can keep this paper up-to-date.

No doubt there will be questions and comments, which I will be happy to deal with on-line.

 

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
 
Sponsored Links