473,399 Members | 3,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Advantages of ICloneable?

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
Nov 17 '05 #1
6 3221
Andy <an*******@hotmail.com> wrote:
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.
You can expose that separately, and implement ICloneable explicitly.
Why should I then use IClonable??


So that things which only care that an object can be cloned can accept
instances of your class as a parameter. Not everything will always know
your concrete type...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2

One standard impelementation is something like this:

public MyClass Clone() { ... }

object ICloneable.Clone { return Clone(); }

That way if the reference is typed as MyClass you can use the strongly
typed Clone method, but if you're referring to the object simply as
ICloneable then you can use the standard interface method.

This implementation gives you the best of both worlds--a strongly
typed Clone() method and also a polymorhpic Clone() method.

HTH,

Sam
On Thu, 26 May 2005 20:46:11 GMT, Andy <an*******@hotmail.com> wrote:
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


Nov 17 '05 #3
Jon Skeet [C# MVP] wrote:
Andy <an*******@hotmail.com> wrote:
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.

You can expose that separately, and implement ICloneable explicitly.

Why should I then use IClonable??

So that things which only care that an object can be cloned can accept
instances of your class as a parameter. Not everything will always know
your concrete type...


That makes sense.

Thank you.
Nov 17 '05 #4
Good tip.

Thank you.

Samuel R. Neff wrote:
One standard impelementation is something like this:

public MyClass Clone() { ... }

object ICloneable.Clone { return Clone(); }

That way if the reference is typed as MyClass you can use the strongly
typed Clone method, but if you're referring to the object simply as
ICloneable then you can use the standard interface method.

This implementation gives you the best of both worlds--a strongly
typed Clone() method and also a polymorhpic Clone() method.

HTH,

Sam
On Thu, 26 May 2005 20:46:11 GMT, Andy <an*******@hotmail.com> wrote:

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


Nov 17 '05 #5
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);
}
}

Nov 17 '05 #6
Bruce Wood <br*******@canada.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: smith | last post by:
I've been making deep copy clones of objects since my second week or so with ..Net (it took me the first week to realize that shallow copies were usually worthless as "clones"). Normally I call my...
1
by: Michael D. Ober | last post by:
In VB 2005, the ICloneable interface requires the following: Class foo Implements ICloneable Public Function Clone() as Object Implements System.ICloneable.Clone ' return new_object of type...
1
by: Hasani | last post by:
I have an object called FileNode and it implements ICloneable which creates a deep copy of the FileNode. The one of the constructors for ArrayList use an ICollection as a paremeter. If I pass that...
2
by: Venkat Venkataramanan | last post by:
Hello: I have an object Users that implements the ICloneable interface. I am trying to pass an instance of this object, foUser from one form into a second form ByRef. Dim formLogin As New...
7
by: Don | last post by:
Can anyone give me an example of implementing ICloneable to give a class I created a "Clone" method so I can make copies of objects. I have no idea where to begin with this. Thanks. - Don
1
by: Rain | last post by:
Hi. Im a C# newbie, just want to ask how to implement the ICloneable.Clone.. Dont know how it works, would really appreciate it if someone could show a simple source sample of how to do this.....
2
by: Nathan | last post by:
I'm working with Clone() for the first time, and noticed that you have to unbox the Clone of an object that implements ICloneable: MyObject var1 = new MyObject(); // Where MyObject implements...
6
by: Stefan Hoffmann | last post by:
hi, the following implementations work: public class NullSafeCollection: System.Collections.CollectionBase, System.ICloneable { object System.IClonable.Clone() { NullSafeCollection clone =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.