我有兩本字典。當我更改字典1中的值時,字典2中會出現相同的更改。如何僅在字典1中更改值,而不是在字典2中更改值?如何複製字典列表
List<Dictionary<string, string>> ld1 = new List<Dictionary<string, string>>();
Dictionary<string, string> d1 = new Dictionary<string,string>();
d1.Add("Text", "Value1");
d1.Add("Format", "Value2");
ld1.Add(d1);
List<Dictionary<string, string>> ld2 = new List<Dictionary<string, string>>(ld1);
// ld2 = ld1
ld1[0]["Text"] = "Eulav"; // should: change only in the first dictionary
// actually: changes in the second dictionary as well
Console.WriteLine(ld1[0]["Text"]);
Console.WriteLine(ld2[0]["Text"]);
輸出
Eulav
Eulav
它的工作原理,謝謝 – Radicz