469,304 Members | 2,192 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,304 developers. It's quick & easy.

Deep copy

Hello,

What is the easiest way to make "deep copy" of my Hashtable?

How about with other Collection classes in C#, any documents available?

I don'r actually understand why Framework's Collection classes does not
support deep copy methods
by theself, only Clone with shallow copy.

bmw
Nov 26 '05 #1
5 25267
On Sat, 26 Nov 2005 16:26:02 +0200, "BenW" <bm*@mailer.smart.com>
wrote:
What is the easiest way to make "deep copy" of my Hashtable?
There is no easy way. You must define your own deep copy method on
the element type of your hashtable, and an additional deep copy method
for your hashtable that calls the element copy method on all elements.
How about with other Collection classes in C#, any documents available?
No documents, same as above. The Framework does not support any deep
copies of collections by default.
I don'r actually understand why Framework's Collection classes does not
support deep copy methods by theself, only Clone with shallow copy.


A standard deep copy support would have to be ubiquitous. Collections
can contain anything, and that "anything" could be a type that
contains other nested objects, and so on. A deep copy must recurse
through all those levels. The Framework designers didn't provide a
default deep copy method on System.Object, hence they couldn't very
well provide one for System.Collections types (how to copy elements?).
--
http://www.kynosarges.de
Nov 26 '05 #2
> What is the easiest way to make "deep copy" of my Hashtable?
The easiest way to deep copy a Hashtable is to use serialization as shown
in the example below.

using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

public class DeepCopyExample
{
public static void Main()
{
Hashtable t1=new Hashtable();
t1.Add("Key1","Value1");
t1.Add("Key2","Value2");
MemoryStream s=new MemoryStream();
BinaryFormatter f=new BinaryFormatter();
f.Serialize(s,t1);
s.Position=0;
Hashtable t2=(Hashtable) f.Deserialize(s);
Debug.Assert(t2["Key"]==t1["Key1"]);
}
}

You can use serialization to deep copy any type that is serializable.

Anders Norås
http://dotnetjunkies.com/weblog/anoras
Nov 26 '05 #3
On Sat, 26 Nov 2005 14:11:41 -0800, Anders Norås
<an**********@objectware.no> wrote:
The easiest way to deep copy a Hashtable is to use serialization as shown
in the example below.


That's a clever method but I wonder about the runtime performance
since you get the overhead of a MemoryStream, and also of member-wise
reflection for the serialization of all fields in all elements.

Of course you could implement ISerializable to avoid the cost of
reflection but in that case your way wouldn't be easy anymore...

By the way, there is actually no difference between a deep copy and a
shallow copy in your example. Your hashtable contains only strings,
and .NET strings are immutable.
--
http://www.kynosarges.de
Nov 27 '05 #4
> That's a clever method but I wonder about the runtime performance
since you get the overhead of a MemoryStream, and also of member-wise
reflection for the serialization of all fields in all elements. As you point out there is a performance overhead with serialization, but
still this is the easiest way to deep copy a serializable object. I often
use serialization to clone objects and I've seldom had any performance problems
when doing this.
Of course you could implement ISerializable to avoid the cost of
reflection but in that case your way wouldn't be easy anymore... Reflection isn't as expensive as many people tell you. Try to use serialization
first and fall back to implementing ISerializable, or IClonable, if you experience
performance problem.
By the way, there is actually no difference between a deep copy and a
shallow copy in your example. Your hashtable contains only strings,
and .NET strings are immutable.

I'm aware of this, but I wanted to keep my example simple. This works for
*any* serializable object and you're guarandteed to get a deep copied clone.

Regards,
Anders Norås
http://dotnetjunkies.com/weblog/anoras/
Nov 27 '05 #5
You must create a clone method that first use MemberwiseClone() method to
create a new cloned object an then, for each field of your class that is a
"reference" (not a struct), you must clone the field recursively. This is
the only way with C# to do an efficient cloning.

Hope it helps.
"BenW" <bm*@mailer.smart.com> a écrit dans le message de
news:O9**************@TK2MSFTNGP10.phx.gbl...
Hello,

What is the easiest way to make "deep copy" of my Hashtable?

How about with other Collection classes in C#, any documents available?

I don'r actually understand why Framework's Collection classes does not
support deep copy methods
by theself, only Clone with shallow copy.

bmw

Nov 28 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Tony Johansson | last post: by
4 posts views Thread by fperfect13 | last post: by
4 posts views Thread by Dennis | last post: by
6 posts views Thread by Desmond Cassidy | last post: by
13 posts views Thread by blangela | last post: by
4 posts views Thread by shuisheng | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.