#827 – Making a Deep Copy with a Copy Constructor
April 22, 2013 4 Comments
The semantics that you use within a copy constructor can be to make a shallow copy of an object or a deep copy.
In the example below, the copy constructor for Dog makes a deep copy of the object passed in. In this case, that means that the new Dog that is created will also have a new instance of DogCollar object, copied from the DogCollar property of the original Dog object.
public class Dog { public string Name { get; set; } public int Age { get; set; } public DogCollar Collar { get; set; } // Constructor that takes individual property values public Dog(string name, int age) { Name = name; Age = age; } // Copy constructor (deep copy) public Dog(Dog otherDog) { Name = otherDog.Name; Age = otherDog.Age; Collar = (otherDog.Collar != null) ? new DogCollar(otherDog.Collar.Length, otherDog.Collar.Width) : null; } }
May I ask…. what is the advantage and disadvantage of implementing the copy constructor and ICloneable.Clone ?
um… With talking about the best practice, which one you think should be more good to be used ?
Cheers,
Elliott
Depends on whether you want your object to obey the semantics of the ICloneable interface. ICloneable only provides a method for copying an object (Clone), but doesn’t allow specifying deep vs. shallow. Copy constructor would suffer from the same problem, though you could add a parameter indicating deep vs. shallow. Or maybe, for your object, it’s obvious whether you want a deep or a shallow copy. In general, interfaces can help you implement polymorphic behavior. I.e. If you have various classes that all implement ICloneable, then you can clone objects in these classes in the same way, regardless of the specific type, by calling ICloneable.Clone. (See https://csharp.2000things.com/2011/10/21/438-benefits-of-using-interfaces/)
For good discussions, see:
http://stackoverflow.com/questions/3345389/copy-constructor-versus-clone
and
http://stackoverflow.com/questions/699210/why-should-i-implement-icloneable-in-c
Should “Name = otherDog.Name;” be “Name = String.Copy(otherDog.Name);” ? In order to make it a deep copy?
I don’t think there’s a need. Since strings are immutable, both objects have the same copy and if you changed the value in otherDog, it wouldn’t affect the copy pointed to by the new object.