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


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. 


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.


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.


Sponsored Links