Home / Programming / Blog article: Fundamentals: Deep Cloning In C#

| RSS

Fundamentals: Deep Cloning In C#

September 1st, 2008 | 2 Comments | Posted in Programming

To make an object cloneable in .NET we should implement ICloneable interface. ICloneable is a simple interface with only one method Clone(). In this post I will show you how to make a deep clone of an object.

class Program
{
    static void Main(string[] args)
    {
        Customer c1 = new Customer
            {
                FirstName = "John",
                LastName = "Smith",
                Limit = new CreditLimit
                {
                    AccountOverdue = false,
                    ApprovedAmount = 1000
                } };
        Customer c2 = (Customer)c1.Clone();
        c2.FirstName = "Deepak";
        c2.LastName = "Kapoor";
        c2.Limit.AccountOverdue = true;
        c2.Limit.ApprovedAmount = 5000;
        Console.WriteLine("C1.FirstName = {0}, C2.FirstName = {1}", c1.FirstName, c2.FirstName);
        Console.WriteLine("C1.LastName = {0}, C2.LastName = {1}", c1.LastName, c2.LastName);
        Console.WriteLine("C1.Limit.AccountOverdue = {0}, C2.Limit.AccountOverdue = {1}",
            c1.Limit.AccountOverdue, c2.Limit.AccountOverdue);
        Console.WriteLine("C1.Limit.ApprovedAmount = {0}, C2.Limit.ApprovedAmount = {1}",
            c1.Limit.ApprovedAmount, c2.Limit.ApprovedAmount);
        Console.ReadKey();
    }
}
[Serializable]
public class Customer : ICloneable
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public CreditLimit Limit { get; set; }
    #region ICloneable Members
    public object Clone()
    {
        object clone;
        using (MemoryStream stream = new MemoryStream())
        {
            BinaryFormatter formatter = new BinaryFormatter();
            // Serialize this object
            formatter.Serialize(stream, this);
            stream.Position = 0;
            // Deserialize to another object
            clone = formatter.Deserialize(stream);
        }
        return clone;
    }
    #endregion
}
[Serializable]
public class CreditLimit
{
    public int ApprovedAmount { get; set; }
    public bool AccountOverdue { get; set; }
}

In the code above I have a Customer class which has three properties. In the clone method I use a MemoryStream to serialize and then de-serialize to another object. One of the properties of customer class is called Limit which is of type CreditLimit. I have done this to illustrated deep cloning. As a test I write property values of the original customer class and its clone to console. Note that I modify values for the cloned object. That’s all you need to do for cloning. Nice and simple.

Leave a Reply 7243 views, 1 so far today |
Tags:
Follow Discussion

2 Responses to “Fundamentals: Deep Cloning In C#”

  1. Amirhossein Says:

    I have a class that inherit another class from a dll that i don’t have Permission to change it and it’s not marked as [Serializable].
    I need to clone an object of this class what should i do?

Trackbacks

  1. Recent Faves Tagged With "serialize" : MyNetFaves  

Leave a Reply





Switch to our mobile site