如何更新值为一个特定的关键字在字典字典<字符串,int>?
当前回答
这个简单的检查将执行upsert,即更新或创建。
if(!dictionary.TryAdd(key, val))
{
dictionary[key] = val;
}
其他回答
这个简单的检查将执行upsert,即更新或创建。
if(!dictionary.TryAdd(key, val))
{
dictionary[key] = val;
}
只需指向字典中给定的键并赋一个新值:
myDictionary[myKey] = myNewValue;
你也可以使用这个方法:
Dictionary<int,int> myDic = new();
if (myDic.ContainsKey(1))
{
myDic[1] = 1234; // or use += to update it
}
或按值:
if (myDic.ContainsValue(1))
{
//do something ...
}
通过将键作为索引访问是可能的
例如:
Dictionary<string, int> dictionary = new Dictionary<string, int>();
dictionary["test"] = 1;
dictionary["test"] += 1;
Console.WriteLine (dictionary["test"]); // will print 2
这里有一种通过索引进行更新的方法,就像foo[x] = 9,其中x是键,9是值
var views = new Dictionary<string, bool>();
foreach (var g in grantMasks)
{
string m = g.ToString();
for (int i = 0; i <= m.Length; i++)
{
views[views.ElementAt(i).Key] = m[i].Equals('1') ? true : false;
}
}
推荐文章
- 实体框架核心:在上一个操作完成之前,在此上下文中开始的第二个操作
- 如何为构造函数定制Visual Studio的私有字段生成快捷方式?
- 如何使用JSON确保字符串是有效的JSON。网
- AppSettings从.config文件中获取值
- 通过HttpClient向REST API发布一个空体
- 如何合并字典的字典?
- 如何检查IEnumerable是否为空或空?
- 自动化invokerrequired代码模式
- 在c#代码中设置WPF文本框的背景颜色
- 在c#中,什么是单子?
- c#和Java中的泛型有什么不同?和模板在c++ ?
- Swift:声明一个空字典
- 将JSON字符串转换为HashMap
- 如何在Python中逐行打印字典?
- c#线程安全快速(est)计数器