How can i copy Dictionary object 开发者_如何学编程by value in c#
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);
Using System.Linq;
Dictionary<int, string> dict2 = dict1.ToDictionary(k => k.Key, k => k.Value.ToString());
This will create an identical copy of a dictionary by value (not ref), so you can operate on one without changing the other.
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.
精彩评论