Advantages of ICloneable?

A

Andy

Hello.

I've got an issue with the ICloneable interface that makes me think I'm
missing the advantages of it.

My issue is that the implemented Clone method returns an object and not
a strongly typed definition. This means that the class you create
exposes a method that returns an object which the user of the class must
cast.

public object Clone() {...}

I would much prefer the class to expose a type of itself:

public MyClass Clone() {...}

so that the caller doesn't have to cast and the interface is cleaner.

Why should I then use IClonable??


Thanks,
Andy
 
B

Bruce Wood

Here's another tip: in an inheritance hierarchy, implementing
ICloneable can be a pain:

public class BaseClass : ICloneable
{
private int _member1;
private string _member2;

public BaseClass(string aString)
{
this._member2 = aString;
this._member1 = 0;
}

public virtual object Clone()
{
BaseClass clone = new BaseClass(this._member2);
clone._member1 = this._member1;
...
}
}

public class DerivedClass : BaseClass
{
}

Now, what you really want to do in DerivedClass is override Clone().
However, there's a problem: you can't get at the fields in the base
class, because they're private, and the base class doesn't necessarily
expose all of those fields as properties, or allow you to set them. So,
how do you clone the derived class (including the base class fields) if
you can't copy the base class fields?

The answer is to make a protected method called CopyTo (or CopyFrom...
it doesn't really much matter), and a protected empty constructor.

public class BaseClass : ICloneable
{
private int _member1;
private string _member2;

public BaseClass(string aString)
{
this._member2 = aString;
this._member1 = 0;
}

protected BaseClass()
{
this._member2 = null;
this._member1 = 0;
}

protected virtual CopyFrom(BaseClass otherBase)
{
this._member1 = otherBase._member1;
this._member2 = otherBase._member2;
...
}

public virtual object Clone()
{
BaseClass clone = new BaseClass();
clone.CopyFrom(this);
}
}

Now the derived class looks similar:

public class DerivedClass : BaseClass
{
private int _member3;
private string _member4;

public DerivedClass(string aString) : base(aString)
{
this._member3 = "";
this._member4 = 0;
}

protected DerivedClass() : BaseClass()
{
this._member3 = null;
this._member4 = 0;
}

protected virtual CopyFrom(DerivedClass otherDerived)
{
base.CopyFrom(otherDerived);
this._member3 = otherDerived._member3;
this._member4 = otherDerived._member4;
...
}

public virtual object Clone()
{
DerivedClass clone = new DerivedClass();
clone.CopyFrom(this);
}
}
 
J

Jon Skeet [C# MVP]

Bruce Wood said:
Here's another tip: in an inheritance hierarchy, implementing
ICloneable can be a pain:

<snip>

Alternatively, where it's appropriate (i.e. for shallow copies where
you have enough access to make any "interesting" bits deep yourself)
you can just use Object.MemberwiseClone.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top