473,396 Members | 2,111 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,396 software developers and data experts.

ICloneable ... why type it out?

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 method "Clone" ... it's just a naming habit I got into
with VB5/6 using LSET on UDTs for serializing state and creating copies of
objects.

The way I do it is of course with .Net is the wya most everyone does it,
mark the class with the Serializable attribute, implement ISserializable
with it's constructor and GetObjectData. The Clone method does the
traditional binaryformater and memory stream serialization and the method
returning an object of the same type as the running instance (though I often
overload the Clone method and use custom serialization to instead convert
the object to another type, such as a state-only object for tighter
remoting). No rule breaking here, ISerializable's "propertybagish" system
is logical.

But, here's the question: In my three years of primarily doing .Net, most of
my clones (found in nearly every one of my apps) never specifically
implemented ICloneable. I'll add it if I'm feelign picky but even when I do
add it, I always just look at that interface and scratch my head ... There
seems to be nothing worthwhile in using it, since I'm doing all of the
actual work myself, using the syntax (albeit out of pre-.Net habit) and
just don't need the "reminder" that I have a Clone method. Also, my Clones
output an actual type rather than an Object.

Forgive my ignorance, but can you tell me a real reason for specifically
typing out the "Implements ICloneable", beyond just "it's what you're
supposed to do"?

There's doing "the right thing" for a good reason and then there's going
overboard ... in this little thing I wonder the basic logic.

Thanks.

Robert Smith
Kirkland, WA
www.smithvoice.com


Jul 21 '05 #1
4 1918
Consistency perhaps? I'd say that people consuming your libraries (if that
were the case) would expect to use a standard CLR interface to achieve
cloning. Not that you couldn't do it the way you describe, but still.

It's kinda like always inheriting from EventArgs when creating complex types
to pass to delegate handlers. You don't have to, but it's nice to "stick" to
the "standard", I guess.
--
Klaus H. Probst, MVP
http://www.vbbox.com/
"smith" <rc********@smithvoiceTAKEOUT.com> wrote in message
news:OJ****************@newsread3.news.pas.earthli nk.net...
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 method "Clone" ... it's just a naming habit I got into
with VB5/6 using LSET on UDTs for serializing state and creating copies of
objects.

The way I do it is of course with .Net is the wya most everyone does it,
mark the class with the Serializable attribute, implement ISserializable
with it's constructor and GetObjectData. The Clone method does the
traditional binaryformater and memory stream serialization and the method
returning an object of the same type as the running instance (though I often overload the Clone method and use custom serialization to instead convert
the object to another type, such as a state-only object for tighter
remoting). No rule breaking here, ISerializable's "propertybagish" system
is logical.

But, here's the question: In my three years of primarily doing .Net, most of my clones (found in nearly every one of my apps) never specifically
implemented ICloneable. I'll add it if I'm feelign picky but even when I do add it, I always just look at that interface and scratch my head ... There seems to be nothing worthwhile in using it, since I'm doing all of the
actual work myself, using the syntax (albeit out of pre-.Net habit) and
just don't need the "reminder" that I have a Clone method. Also, my Clones output an actual type rather than an Object.

Forgive my ignorance, but can you tell me a real reason for specifically
typing out the "Implements ICloneable", beyond just "it's what you're
supposed to do"?

There's doing "the right thing" for a good reason and then there's going
overboard ... in this little thing I wonder the basic logic.

Thanks.

Robert Smith
Kirkland, WA
www.smithvoice.com

Jul 21 '05 #2
Thanks Klaus (and good to talk to you again). I do understand the tradition
of it and I grasp the idea (while Inheriting and implementing an interface
are a little bit different) but this particual interface just strikes me as
less than real-world logical for some reason.

Perhaps the aspect that bothers me about it most is that the return type of
the interface's Clone method is "Object" when it seems to me a Clone should
return an object of the exact type of the source and casting shouldn't be
needed; Granted the only way to generalize is to make the return an "Object"
but since in this case there's only just the one method in the interface ...
oy, Even right now as I try to just accept it as "what you should do because
that's how it's done" it just continues to look like a silly interface.

It's not that it's a major deal, my code hasn't failed over it and others do
tap into my dlls. I just see newer devs just catching the .Net religion and
following little things like this as if they're some rule from God and
wonder if there's some tangible performance issue I've been missing out on.

Thanks.

-smith
www.smithvoice\.com

"Klaus H. Probst" <us*******@vbbox.com> wrote in message
news:e5**************@TK2MSFTNGP14.phx.gbl...
Consistency perhaps? I'd say that people consuming your libraries (if that
were the case) would expect to use a standard CLR interface to achieve
cloning. Not that you couldn't do it the way you describe, but still.

It's kinda like always inheriting from EventArgs when creating complex
types
to pass to delegate handlers. You don't have to, but it's nice to "stick"
to
the "standard", I guess.
--
Klaus H. Probst, MVP
http://www.vbbox.com/

Jul 21 '05 #3
>
It's not that it's a major deal, my code hasn't failed over it and others
do tap into my dlls. I just see newer devs just catching the .Net
religion and following little things like this as if they're some rule
from God and wonder if there's some tangible performance issue I've been
missing out on.


No performance issues, just potential consistency and compatibility issues.
Most interfaces exist for the benefit of API's and\or languages(usually
api's). It is far easier, faster, and safer to determine Clone generically
via an ICloneable interface than it is, say, via reflection.
If you had a generic object manager(not specialized to a specific tree) that
clones objects to maintain a pool or a language that provides a "copy" or
"clone" keyword, both of those would likely rely on ICloneable in a generic
sense, or would have anyway.

The issue with ICloneable is that it doesn't specify deep or shallow copy.
Its use is...less valuable that the designers probably intended. I would
pretty strongly suggest foregoing ICloneable and using your own cloning
method\interface to specify clone functionality. Just remember to specify if
the method is deep, shallow, or object-specific. Being unsure of
expectations is a considerable problem.

I would certainly, however, recommend an interface or common base class
instead of just adding Clone methods everywhere. This may not be feasible
until the 2.0 release, however.

Jul 21 '05 #4
Always sataisfying to talk things out. Thank you Daniel.
-s

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:%2****************@TK2MSFTNGP11.phx.gbl...
No performance issues, just potential consistency and compatibility
issues. Most interfaces exist for the benefit of API's and\or
languages(usually api's). It is far easier, faster, and safer to determine
Clone generically via an ICloneable interface than it is, say, via
reflection.
If you had a generic object manager(not specialized to a specific tree)
that clones objects to maintain a pool or a language that provides a
"copy" or "clone" keyword, both of those would likely rely on ICloneable
in a generic sense, or would have anyway.

The issue with ICloneable is that it doesn't specify deep or shallow copy.
Its use is...less valuable that the designers probably intended. I would
pretty strongly suggest foregoing ICloneable and using your own cloning
method\interface to specify clone functionality. Just remember to specify
if the method is deep, shallow, or object-specific. Being unsure of
expectations is a considerable problem.

I would certainly, however, recommend an interface or common base class
instead of just adding Clone methods everywhere. This may not be feasible
until the 2.0 release, however.

Jul 21 '05 #5

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

Similar topics

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...
6
by: Andy | last post by:
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...
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
8
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.