Connecting Tech Pros Worldwide Help | Site Map

Deep copy

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 26th, 2005, 01:35 PM
BenW
Guest
 
Posts: n/a
Default 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



  #2  
Old November 26th, 2005, 02:45 PM
Christoph Nahr
Guest
 
Posts: n/a
Default Re: Deep copy

On Sat, 26 Nov 2005 16:26:02 +0200, "BenW" <bmw@mailer.smart.com>
wrote:
[color=blue]
>What is the easiest way to make "deep copy" of my Hashtable?[/color]

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.
[color=blue]
>How about with other Collection classes in C#, any documents available?[/color]

No documents, same as above. The Framework does not support any deep
copies of collections by default.
[color=blue]
>I don'r actually understand why Framework's Collection classes does not
>support deep copy methods by theself, only Clone with shallow copy.[/color]

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
  #3  
Old November 26th, 2005, 09:25 PM
Anders Norås
Guest
 
Posts: n/a
Default Re: Deep copy

> 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


  #4  
Old November 27th, 2005, 08:35 AM
Christoph Nahr
Guest
 
Posts: n/a
Default Re: Deep copy

On Sat, 26 Nov 2005 14:11:41 -0800, Anders Norås
<anders.noras@objectware.no> wrote:
[color=blue]
>The easiest way to deep copy a Hashtable is to use serialization as shown
>in the example below.[/color]

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
  #5  
Old November 27th, 2005, 11:05 AM
Anders Norås
Guest
 
Posts: n/a
Default Re: Deep copy

> That's a clever method but I wonder about the runtime performance[color=blue]
> since you get the overhead of a MemoryStream, and also of member-wise
> reflection for the serialization of all fields in all elements.[/color]
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.
[color=blue]
> Of course you could implement ISerializable to avoid the cost of
> reflection but in that case your way wouldn't be easy anymore...[/color]
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.
[color=blue]
> 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.[/color]
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/


  #6  
Old November 28th, 2005, 07:35 AM
Ludovic SOEUR
Guest
 
Posts: n/a
Default Re: Deep copy

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" <bmw@mailer.smart.com> a écrit dans le message de
news:O9aqEVp8FHA.2800@TK2MSFTNGP10.phx.gbl...[color=blue]
> 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
>
>[/color]


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.