2013-03-24 74 views
0

我有一个字典对象:asp.net字典<字符串,字符串>更新项基于键值

protected Dictionary<string,string> myDict; 

我想能够搜索字典经由键和更新的该值键/值对。

if (!myDict.ContainsKey(key)) 
{ 
    myDict.Add(key, value); //if key is not found in the collection add it. 
} 
else //if it is found then update it. 
{ 
    Update[Key].Value with myValue 
} 

有没有办法做到以上几点?如何通过检查密钥来更新密钥的值? 在else语句中匹配伪代码的实际代码是什么?

回答

3

你可以做

myDict[key] = value; 

如果字典已包含密钥,则会进行更新。否则,它被创建。

+0

啊是的。我正在尝试myDict.Keys [键],这就是为什么它不起作用。语法错了。谢谢。 – 2013-03-24 14:47:14

0

应该是简单的,如果我理解正确:

if (!myDict.ContainsKey(key)) 
{ 
    myDict.Add(key, value); //if key is not found in the collection add it. 
} 
else //if it is found then update it. 
{ 
    myDict[key] = value; 
} 
2

你可以找到钥匙的价值和分配新值:

myDict[myKey] = yourNewValue; 
相关问题