Daily Archives: August 1st, 2008

WikiPedia has a good article related to Object Copying. But I dont think it is complete.

Usually we come across a situation where we as a developer need to make a decision when working with hashtables/arrays/collections,  I.E whether we deep copy or shallow copy.

The way C# does copying is thru the copy constructor, doing one of three ways of copying

  • Reference Copy
  • Shallow Copy
  • Deep Copy

Very simply defining shallow copy – It is copying the structure of the object ( the Skeleton)

And defining deep copy – Is copying the structure as well as the data.

A good Explanation…More Info

Getting back to implementing a deep copy in your C# classes, you can employ the use of the ICloneable Interface, or yoeu can also create your own implmentation of deeep copies.

This is a debateable topic and many developers think differently. It is something I might have to try eventually.

Route without the use ICloneable Interface. (Possibly implementing your own Clone method)

Route with ICloneable Interface. (Greate article by InformIT, Also explains the C++ use)

I am open to any questions and comments.

For all the avid C# developers – a must look at resource.

C# Online.NET