简体   繁体   English

复制字典没有给我预期的结果

[英]copying dictionary doesn't give me expected result

I have to copy one dictionary, work with that copy and return to the original one. 我必须复制一本字典,使用该副本并返回原始字典。

What seems to happen is that the orignal dictionary is modified when I do some work on the copied one. 似乎发生的是,当我对复制的字典进行某些工作时,原始字典已被修改。

Here is my code : 这是我的代码:

dmodified_profile = new SortedDictionary<int,SortedDictionary<string,List<string>>>(d_profile);

I don't know why d_profile which is the original one could be modified if my modifications are done on the dmodified_profile dictionary ? 我不知道如果对dmodified_profile词典进行修改,为什么可以修改原始的d_profile?

Thanks 谢谢

您需要深层复制

Your SortedDictionary maps an integer to a REFERENCE to another SortedDictionary. 您的SortedDictionary将整数映射到对另一个SortedDictionary的引用。 When you copy that dictionary you copy the values of the keys as well as the REFERENCEs of the values, because your dictionary's value is of a reference type. 当您复制该字典时,您将复制键的值以及这些值的引用,因为字典的值属于引用类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM