473,608 Members | 2,264 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

deep copy (Clone) on System.Collecti ons.Generic.Dic tionary ?

Hi,
what is the best way to perform deep copy (Clone) on class that inherit from System.Collecti ons.Generic.Dic tionary ?
Dec 19 '06 #1
3 16754
Serialize and deserialise to a memory stream
Use a binary formater, its faster. You may have to implement a surrogate if
it isnt serialiazable and the contents will need to be too.
--
Ciaran O''''Donnell
http://wannabedeveloper.spaces.live.com
"semedao" wrote:
Hi,
what is the best way to perform deep copy (Clone) on class that inherit from System.Collecti ons.Generic.Dic tionary ?
Dec 19 '06 #2
Hi,

If either the keys or values aren't serializable, or if you don't know
whether they will be, then iterate over the key-value pairs and copy them
into a new instance of your class. If the keys or values are ICloneable
then store the result of the Clone method, otherwise check whether they are
serializable and serialize/deserialize them as per Ciaran's response.

--
Dave Sexton

"semedao" <se*****@commun ity.nospamwrote in message
news:eW******** ******@TK2MSFTN GP06.phx.gbl...
Hi,
what is the best way to perform deep copy (Clone) on class that inherit from
System.Collecti ons.Generic.Dic tionary ?
Dec 19 '06 #3
Hi,

I should add that if you have typed the key and value as value-types then
you'll just have to iterate the dictionary and copy them into a new
instance - no ICloneable or serialization/deserialization required.

--
Dave Sexton

"semedao" <se*****@commun ity.nospamwrote in message
news:eW******** ******@TK2MSFTN GP06.phx.gbl...
Hi,
what is the best way to perform deep copy (Clone) on class that inherit from
System.Collecti ons.Generic.Dic tionary ?
Dec 19 '06 #4

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

Similar topics

4
52288
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) 10.What's the difference between the System.Array.CopyTo() and System.Array.Clone()?
1
2331
by: oDDskOOL | last post by:
I realized today that the Hashtable.Clone only produces a shallow copy... that makes me go mad that M$ doesn't even provide a deep copy ctor for the Hashtable class ! mighty tech ducks might reply "oh but what if the types in hastable don't have copy ctors defined? that could lead to dangerous situations". well, but was it a significant effort to provide a copy ctor / deep clone method to this class for objects that provide the...
4
12440
by: Dennis | last post by:
I have several Data Structures, say "mystruct" which contain arrays of bytes, other structures, etc. I then dimension a variable (var1) as "mystruct" and set the various elements var1 to data. I then dimension another variable (var1save) as "mystruct" and set var1save = var1 with the intent of changing var1 data elements and subsequently reasigning var1 back to it's original values of var1save. VB.Net makes only a shallow copy of var1...
5
25638
by: BenW | last post by:
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.
4
3031
by: jjkboswell | last post by:
I have an XSD which I have generated a class from using the xsd.exe tool. My XSD contains complex types within it, so that the generated class has member variables which are of types that are also defined in the XSD. I deserialize XML data into instances of that class, and serialize back to XML. So far so good. However, in my code I have the concept of wanting to create new copies of these deserialized objects and modifying some of...
2
10100
by: bonk | last post by:
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/2004/05/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".
4
2768
by: Kyote | last post by:
I'm trying to persist a list of filenames. I've made a custom collection and a FileName class: 'Class to hold file name information Public Class FileNames Public fullName As String Public fileName As String Public fileExtention As String Public filePath As String Public newName As String
3
8744
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 format, 3 pp) My question, coming from a C++ background where deep copying is done, is why in C# you would do either deep or shallow copying as suggested by O'Reilly (using the "ICloneable" inhereited interface), at least for the .NET framework. ...
3
2073
by: gasfusion | last post by:
Hey guys. I have a little problem trying to clone my object, which is shown below. If you notice below, i am using a generic linked list structure to store byte arrays. The problem is when i use my Clone() method to clone this object, it doesn't clone the actual LinkedList. Any ideas on how to go about fixing this? Much appreciated. using System; using System.Reflection; using System.Collections.Generic; namespace Compiler { ...
0
8063
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8496
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8475
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8338
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6816
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6013
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5475
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2474
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 we have to send another system
0
1329
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.