I wish to implement a deepcopy of my classes hierarchy in C#
public Class ParentObj : ICloneable
{
protected int myA;
public virtual Object Clone
The typical approach is to use "copy constructor" pattern a la C++:
class Base : ICloneable
{
int x;
protected Base(Base other)
{
x = other.x;
}
public virtual object Clone()
{
return new Base(this);
}
}
class Derived : Base
{
int y;
protected Derived(Derived other)
: Base(other)
{
y = other.y;
}
public override object Clone()
{
return new Derived(this);
}
}
The other approach is to use Object.MemberwiseClone
in the implementation of Clone
- this will ensure that result is always of the correct type, and will allow overrides to extend:
class Base : ICloneable
{
List xs;
public virtual object Clone()
{
Base result = this.MemberwiseClone();
// xs points to same List object here, but we want
// a new List object with copy of data
result.xs = new List(xs);
return result;
}
}
class Derived : Base
{
List ys;
public override object Clone()
{
// Cast is legal, because MemberwiseClone() will use the
// actual type of the object to instantiate the copy.
Derived result = (Derived)base.Clone();
// ys points to same List object here, but we want
// a new List object with copy of data
result.ys = new List(ys);
return result;
}
}
Both approaches require that all classes in the hierarchy follow the pattern. Which one to use is a matter of preference.
If you just have any random class implementing ICloneable
with no guarantees on implementation (aside from following the documented semantics of ICloneable
), there's no way to extend it.