按值复制字典

hip*_*out 18 c# dictionary

如何在c#中按值复制Dictionary对象

Meh*_*ari 33

Dictionary在构造函数中创建一个新的传递源字典(当然,如果它们是引用类型,这将不会复制字典中的对象):

var copied = new Dictionary<KeyType, ValueType>(originalDictionary);
Run Code Online (Sandbox Code Playgroud)


Aar*_*ron 5

Using System.Linq;
Dictionary<int, string> dict2 = dict1.ToDictionary(k => k.Key, k => k.Value.ToString());
Run Code Online (Sandbox Code Playgroud)

这将按值(而不是ref)创建字典的相同副本,因此您可以在不更改另一个的情况下对其进行操作.