Posts

Showing posts from December, 2010
Image
  My dear Sachin... I love u :-)

C# Cloning

Introduction "Introduction Cloning C# objects is one of those things that appears easy but is actually quite complicated with many 'gotchas.' This articl..." Cloning C# objects is one of those things that appears easy but is actually quite complicated with many "gotchas." This article describes the most common ways to clone a C# object. Shallow vs. Deep Cloning There are two types of object cloning: shallow and deep. A shallow clone copies the references but not the referenced objects. A deep clone copies the referenced objects as well. Hence, a reference in the original object and the same reference in a shallow-cloned object both point to the same object. Whereas a deep-cloned object contains a copy of everything directly or indirectly referenced by the object. ICloneable Interface The ICloneable interface contains a single Clone method, which is used to create a copy of the current object. public interface ICloneable { object Clone(); } The problem wi...