#143 – An Example of Implementing ICloneable for Deep Copies

Here’s an example of implementing ICloneable in two custom classes so that you can use the Clone method to do a deep copy.

To do a deep copy of the Person class, we need to copy its members that are value types and then create a new instance of Address by calling its Clone method.

        public class Person : ICloneable
        {
            public string LastName { get; set; }
            public string FirstName { get; set; }
            public Address PersonAddress { get; set; }

            public object Clone()
            {
                Person newPerson = (Person)this.MemberwiseClone();
                newPerson.PersonAddress = (Address)this.PersonAddress.Clone();

                return newPerson;
            }
        }

The Address class uses MemberwiseClone to make a copy of itself.

        public class Address : ICloneable
        {
            public int HouseNumber { get; set; }
            public string StreetName { get; set; }

            public object Clone()
            {
                return this.MemberwiseClone();
            }
        }

Cloning a Person:

            Person herClone = (Person)emilyBronte.Clone();
Advertisements

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

10 Responses to #143 – An Example of Implementing ICloneable for Deep Copies

  1. Dipmalya Majumder says:

    Great example Sean.

  2. Pingback: deep copying userControl | BlogoSfera

  3. David Galehouse says:

    You nailed this one Sean!

  4. Seb C. says:

    Thanks, was very clear and usefull 🙂

  5. Ema C says:

    What if instead of an Address object you had a list of Addresses? How would you copy them all? I was thinking about a for or a foreach but that would impact on performance.

    • Sean says:

      You could use Array.Clone, or if List, do a foreach or implement an extension method like:

      public static IList Clone(this IList listToClone) where T: ICloneable
      {
      return listToClone.Select(item => (T)item.Clone()).ToList();
      }

  6. Dave says:

    Hey Sean,

    Thanks for the example regarding the list above.

    I have a ObservableCollection which has a Children property.
    If I clone the list using the extension you provided, the list is cloned but if I modify any of its children, then the original ObservableCollection is modified as well.

    e.g.
    —–
    var clonedList = ObjectCloner.SeanClone(list);

    clonedList[0].Description = “test”; // original list in not affected
    clonedList[0].Children[0].Description = “test” // this affects the original list
    —–

    I’m guessing I need some sort of recursion but I’m not getting anywhere :/ Something like:
    —–
    var clonedList = _CloneEverything(list);
    —–
    Any ideas what the content of _CloneEverything should be?

    Thanks, Dave

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: