如何更新字典在C#中的一个关键的价值? [英] How to update value of a key in dictionary in c#?

查看:186
本文介绍了如何更新字典在C#中的一个关键的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#下面的代码,基本上它是一个简单的字典,某些键和值

I have the following code in c# , basically it's a simple dictionary with some keys and their values.

Dictionary<string, int> dictionary =
    new Dictionary<string, int>();
dictionary.Add("cat", 2);
dictionary.Add("dog", 1);
dictionary.Add("llama", 0);
dictionary.Add("iguana", -1);



我想更新密钥'猫'的新值 5 。结果
我怎么能这样做呢?

I want to update the key 'cat' with new value 5.
How could I do this?

推荐答案

你有没有尝试过

dictionary["cat"] = 5;



:)

:)

更新

dictionary["cat"] = 5+2;
dictionary["cat"] = dictionary["cat"]+2;
dictionary["cat"] += 2;

谨防不存在的键的的:)

这篇关于如何更新字典在C#中的一个关键的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆