C# asp.net字典<;字符串,字符串>;基于键值更新项目

C# asp.net字典<;字符串,字符串>;基于键值更新项目,c#,asp.net,collections,dictionary,C#,Asp.net,Collections,Dictionary,我有一个字典对象: protected Dictionary<string,string> myDict; 有没有办法做到这一点?如何通过检查密钥来更新密钥的值? 与else语句中的伪代码相匹配的实际代码是什么?如果我理解正确,应该很简单: if (!myDict.ContainsKey(key)) { myDict.Add(key, value); //if key is not found in the collection add it. } else //if it

我有一个字典对象:

protected Dictionary<string,string> myDict;
有没有办法做到这一点?如何通过检查密钥来更新密钥的值?
与else语句中的伪代码相匹配的实际代码是什么?

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

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;
}

您可以使用键查找值并指定新值:

myDict[myKey] = yourNewValue;
你能行

myDict[key] = value;

如果字典已包含键,则会更新该键。否则,它将被创建。

啊,是的。我在试我的dict.Keys[钥匙]这就是它不起作用的原因。语法错误。谢谢。
myDict[key] = value;