1 public void aa(Dictionary<string, string> bb){ 2 foreach (var b in bb) { 3 b.value="aaa"; 4 } 5 }
这样提示我b.value是只读的?有什么办法解决吗?
我给你提供2种(value属性是只读的)
1:KeyValuePair<
string
, string
> 用这个对象去遍历
2:用bb[b.key]用key去赋值(直接覆盖,没有就添加)
先Remove,再Add。
就是不能修改了
正解
这里是自己写的忘楼主采纳:
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做为修改,因为遍历过程中不能修改集合