How can i copy Dictionary object by value in c#
Asked
Active
Viewed 1.7k times
24
-
Do you mean a deep copy (i.e. making copies of all the objects in the dictionary as well)? – Paolo Jan 16 '10 at 21:46
3 Answers
43
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
- 414,610
- 91
- 852
- 789
-2
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
- 101,701
- 37
- 181
- 258
-
3Warning! Just because a class implements `ICloneable` does not mean that `Clone` performs a deep copy. – jason Jan 16 '10 at 21:51