473,323 Members | 1,550 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,323 software developers and data experts.

IClonable deep vs shallow, best practise

I have come across the need to distinguish between the creation of a
deep and a shallow copy and with great interest I have read this
article:

http://blogs.msdn.com/brada/archive/...03/125427.aspx

This artivle seems to hint that I should not use System.IClonable but
instead define my own interface(s) for cloning. Now since this article
is rather old and since they did not obsolete IClonable there might be
a new "best practise".

How do I implement Cloning of objects correctly if I want to explicitly
distinguish between deep and shallow copies? Should I

a) use System.IClonable for deep copies and override
object.MemberwiseClone for shallow copies ?

b) define my own IClonable, for example like this:
interface IClonable <T>
{
/// <summary>
/// creates a deep or shallow copy of the current object
/// </summary>
/// <param name="deep">
/// if <c>true</c> a deep copy should be returned,
/// wich means that the copied object and all
/// objects referenced by the object are copied recursively
/// if <c>false</c>a shallow copy should be retruned,
/// wich means that only the top level references
/// are copied
///</param>
/// <returns>a deep or a shallow copy of the current
object</returns>
T Clone (bool deep);
}

c) do something else I did not think of yet ?

Jun 23 '06 #1
2 10043
I don't know of any best practises, but personally I would go with a)

/claes

"bonk" <sc******************@gmx.de> wrote in message
news:11*********************@p79g2000cwp.googlegro ups.com...
I have come across the need to distinguish between the creation of a
deep and a shallow copy and with great interest I have read this
article:

http://blogs.msdn.com/brada/archive/...03/125427.aspx

This artivle seems to hint that I should not use System.IClonable but
instead define my own interface(s) for cloning. Now since this article
is rather old and since they did not obsolete IClonable there might be
a new "best practise".

How do I implement Cloning of objects correctly if I want to explicitly
distinguish between deep and shallow copies? Should I

a) use System.IClonable for deep copies and override
object.MemberwiseClone for shallow copies ?

b) define my own IClonable, for example like this:
interface IClonable <T>
{
/// <summary>
/// creates a deep or shallow copy of the current object
/// </summary>
/// <param name="deep">
/// if <c>true</c> a deep copy should be returned,
/// wich means that the copied object and all
/// objects referenced by the object are copied recursively
/// if <c>false</c>a shallow copy should be retruned,
/// wich means that only the top level references
/// are copied
///</param>
/// <returns>a deep or a shallow copy of the current
object</returns>
T Clone (bool deep);
}

c) do something else I did not think of yet ?

Jun 23 '06 #2
Bonk,

See inline:
a) use System.IClonable for deep copies and override
object.MemberwiseClone for shallow copies ?
I would not recommend overriding MemberwiseClone. It's not going to get
you anything really. It's also very specific in what it does.
b) define my own IClonable, for example like this:
interface IClonable <T>
{
/// <summary>
/// creates a deep or shallow copy of the current object
/// </summary>
/// <param name="deep">
/// if <c>true</c> a deep copy should be returned,
/// wich means that the copied object and all
/// objects referenced by the object are copied recursively
/// if <c>false</c>a shallow copy should be retruned,
/// wich means that only the top level references
/// are copied
///</param>
/// <returns>a deep or a shallow copy of the current
object</returns>
T Clone (bool deep);
}
Making cloning interfaces generic isn't the best idea. Most frameworks
that would take advantage of this don't care what T is, they just need a
copy.
c) do something else I did not think of yet ?


I think that the advice in the article is still the best piece of advice
that there is for your specific need (to determine whether or not it can do
a deep clone or a shallow clone). Basically, define a IDeepClonable
interface, and then a IShallowClonable interface.

There is one problem though. If you have an object that has references
to other objects and you do a deep copy, what do you do when those other
objects don't support deep-copy semantics?

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
Jun 23 '06 #3

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

Similar topics

5
by: Tony Johansson | last post by:
Hello! I'm reading in a book about C++ and that is something that sound strange. It says "Pointers have reference-assignment semantics similar to those in Java. For example, after the...
2
by: Alex | last post by:
Entering the following in the Python shell yields >>> help(dict.copy) Help on method_descriptor: copy(...) D.copy() -> a shallow copy of D >>>
5
by: Zürcher See | last post by:
If I want to implement an IClonable interface, how can I copy the events from my source object to the target "cloned" object?
4
by: fperfect13 | last post by:
Hi, I wanted to perform a deep copy of an array. Searching on google I ran into different opinions : C# Interview Questions (http://blogs.wwwcoder.com/tsvmadhav/archive/2005/04/08/2882.aspx)...
26
by: saxenavaibhav17 | last post by:
what is Deep Copy, Shallow copy and Bitwise copy, Memberwise copy? and what is the difference between them? pls help vaibhav
5
by: pauldepstein | last post by:
I recently had a job interview question which I totally failed. (The question seemed excellent from an objective point of view, but having completely failed to do it, my subjective feelings are...
13
by: blangela | last post by:
I have decided (see earlier post) to paste my Word doc here so that it will be simpler for people to provide feedback (by directly inserting their comments in the post). I will post it in 3 parts...
4
by: shuisheng | last post by:
Dear All, Is there any easy way to make sure all my object copies are deep copy or shallow copy? I do not like to implement it in each class one by one. Thanks, Shuisheng
3
by: raylopez99 | last post by:
The "C# Cookbook" (O'Reilly / Jay Hilyard), section 3.26, is on deep cloning versus shallow cloning. The scanned pages of this book are found here: http://www.sendspace.com/file/mjyocg (Word...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.