Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Framework  »  Copying vs. Cloning
Copying vs. Cloning
by Phil Eakins | Published  02/09/2006 | .NET Framework | Rating:
Phil Eakins
Having followed a successful career in the legal profession for over 20 years, Phil saw the light when he bought his first home computer (an Oric) in 1982. Since then he has attained a Batchelors Degree in Computing and Maths from The Open University (and met his Wife at the same time)as well as attaining a CNE qualification. Phil has worked extensively in the fields of Network Engineering and in the teaching and training of IT subects to adults.  

View all articles by Phil Eakins...
Introduction

Introduction

This was the problem. I had filled an array of Char, Array1()with data and I wanted to sort it.  However, I also wanted to keep a copy of the unsorted data to revert to. 

So,

The unexpected result of this code was that both Array1() and Array2() were sorted simultaneously!

As I later discovered, I was the victim of what is called a ‘shallow’ copy, and yes, there is a ‘deep’ copy which I will discuss, and demonstrate, later. But how and why did this happen? The answer, I’m afraid, lies deep in the Object hierarchy and in how Objects are stored in memory.

Objects

This is a very quick primer on how Objects inter-relate. Please feel free to skip the next few lines if I’m repeating what you already know.

We start by saying that (almost) everything in .NET is an Object (capitalisation intentional) – not just something we can describe as an object, but is derived from the Framework Class System.Object. The Object Class is the ultimate parent of each and every Object contained either within the Framework or which is derived from it.

Unfortunately, below the System.Object level, things get a bit more complicated and the idea of an Object having a Type must be introduced.

 

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