Deep copy

B

BenW

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
 
C

Christoph Nahr

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?).
 
?

=?iso-8859-1?Q?Anders=20Nor=e5s?=

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/anora
 
C

Christoph Nahr

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.
 
?

=?iso-8859-1?Q?Anders=20Nor=e5s?=

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/
 
L

Ludovic SOEUR

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top