Back to Basics – Cloning

September 29, 2008

no comments

Back to Basics – Cloning

In the following posts
I’m going to describeCloning in .Net
basic tools which every
developer needs to
know. In today’s post
I’m going to explain
what is cloning and how
to implement cloning in your application.

What is Cloning?

Cloning is the ability to make an exact copy of an instance of a type.
There are two known forms of cloning:

  • Shallow copy – all the fields of the original object are being copied to the
    copied object and the two objects reference the same objects in the
    end of the copy process.
  • Deep copy – all the fields of the original object are being copied to the
    copied object but the two objects don’t reference the same objects in the
    end of the copy process.

How to Implement Cloning?

Basically, There is an ICloneable interface in the .Net framework but it’s
not recommended to implement it because it doesn’t indicate which method
of cloning you implement.
I prefer using two “home made” generic interfaces like the followings:

    public interface IDeepCopy<T>

    {

        T DeepCopy();

    }

    public interface IShallowCopy<T>

    {

        T ShallowCopy();

    }

Using the two interfaces you can distinct between a shallow copy and deep copy.
Lets look at an example of how to implement the interfaces:

    [Serializable]

    public class ClonedClass : IShallowCopy<ClonedClass>,
                               IDeepCopy<ClonedClass>

    {

        #region Members

        private int _simpleType = 10;

        private List<string> _list = new List<string>();

        #endregion

        #region IShallowCopy<ClonedClass> Members

        public ClonedClass ShallowCopy()

        {

            return (ClonedClass)MemberwiseClone();

        }

        #endregion

        #region IDeepCopy<ClonedClass> Members

        public ClonedClass DeepCopy()

        {

            ClonedClass cloneResult;

            BinaryFormatter formatter = new BinaryFormatter();

            using (MemoryStream stream = new MemoryStream())

            {

                formatter.Serialize(stream, this);

                stream.Flush();

                stream.Seek(0, SeekOrigin.Begin);

                cloneResult = (ClonedClass)formatter.Deserialize(stream);

            }

            return cloneResult;

        }

        #endregion

    }

Pay attention, the DeepCopy implementation I supplied requires the use of
the Serialization attribute in order to work.
The DeepCopy method could be implemented using reflection but I chose this
implementation instead.

Summary

Lets sum up, the use of cloning is one of the basic tools a developer need to know.
It is used widely and has more implementations then the one I showed.
I prefer to avoid the use of ICloneable interface and implement my own interfaces in
order to be more strict and understandable.  

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>