【发布时间】:2013-06-30 21:16:37
【问题描述】:
我有四个 Dictionary ,两个是(字典中的字典),声明如下
Dictionary<string, Dictionary<string, string>> dict_set = new Dictionary<string, Dictionary<string, string>>();
Dictionary<string, Dictionary<string, string>> dict_Reset = new Dictionary<string, Dictionary<string, string>>();
Dictionary<string, string> set_value = new Dictionary<string, string>();
Dictionary<string, string> Reset_value = new Dictionary<string, string>();
我想先在字典 set_vlaue 和 Reset_value 中添加元素。 添加值后,我会将这些字典添加到其他两个字典中,如下所示。
dict_set.Add(condiName, set_value);
dict_Reset.Add(condiName, Reset_value);
set_value.Clear();
Reset_value.Clear();
值正在被添加,但是在添加 set_value 和 reset_value 字典之后,我想清除这两个字典 set_value 和 reset_value,但是当 set_value 和 reset_value 被清除时,dict_set 和 dict_reset 中的数据也被清除..
任何人都可以帮助我,在这种情况下如何创建字典的深层副本...
【问题讨论】:
-
在这里查看 Jon Skeet 的回答 stackoverflow.com/questions/139592/…
-
@keyboardP 实际上我想在执行以下操作时制作字典的深层副本.. dict_set.Add(condiName, set_value); dict_Reset.Add(condiName, Reset_value);一完成我想清除set_value和reset_value..以便可以添加新值..但是根据您的代码,如果我在添加之前克隆它们,一个键将具有其他键数据..
-
@KeyboardP:Skeet 回答的一个问题是它执行叶子的“克隆”副本,这不能保证深度复制。这里的作者想要一个深刻的,所以这不是完全重复的。仍然使用 Skeet 的答案,很容易找到解决方案。
-
另请注意,Skeet 在第一条评论中链接的主题中有 两个 答案。