views:

96

answers:

2

How can i copy Dictionary object by value in c#

+1  A: 

Create a new Dictionary passing the source dictionary in the constructor (of course, this will not copy the objects in the dictionary if they are reference types):

var copied = new Dictionary<KeyType, ValueType>(originalDictionary);
Mehrdad Afshari
thanks, it's work nice
hippout
A: 

There is no intrinsic method to do that. You'll have to do it manually.

The first step is to decide how deep you want the copy to pair. Do you want separate Dictionary entry object holding references to common keys & values, Or do you want everything copied. If the latter, they would need to implement ICloneable to create a general purpose method.

James Curran
Warning! Just because a class implements `ICloneable` does not mean that `Clone` performs a deep copy.
Jason