473,473 Members | 1,524 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 10061
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: 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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
1
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.