首页 新闻 赞助 找找看

Dictionary<string, string>的复制问题

0
悬赏园豆:10 [已解决问题] 解决于 2016-02-25 16:41
1 public void aa(Dictionary<string, string> bb){
2       foreach (var b in bb)   {
3               b.value="aaa";
4     }
5 }

这样提示我b.value是只读的?有什么办法解决吗?

最爱吃糖了的主页 最爱吃糖了 | 初学一级 | 园豆:41
提问于:2015-10-22 15:28
< >
分享
最佳答案
0

我给你提供2种(value属性是只读的)

1:KeyValuePair<string, string> 用这个对象去遍历

2:用bb[b.key]用key去赋值(直接覆盖,没有就添加)

收获园豆:2
稳稳的河 | 老鸟四级 |园豆:4216 | 2015-10-22 15:43
其他回答(3)
0

先Remove,再Add。

Firen | 园豆:5385 (大侠五级) | 2015-10-22 15:47

就是不能修改了

支持(0) 反对(0) 最爱吃糖了 | 园豆:41 (初学一级) | 2015-10-23 10:26
0

正解

茂茂 | 园豆:2892 (老鸟四级) | 2015-10-22 17:28
0

这里是自己写的忘楼主采纳:

Dictionary<string, string> list = new Dictionary<string, string>();
list.Add("1","ss");
list.Add("2", "ss");
list.Add("3", "ss");
aa(list);

 

public static void aa(Dictionary<string, string> bb) {

Dictionary<string, string> temp = new Dictionary<string, string>();
foreach (var item in bb)
{
temp[item.Key] ="23";
}
}

这里返回的temp就是修改后的,这里不能直接用bb做为修改,因为遍历过程中不能修改集合

收获园豆:8
Lasthelloworld | 园豆:92 (初学一级) | 2016-01-07 11:31
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册