C# Dictionary键值对 创建,增加,删除,清空

VS-C++ 系列:所有相关C++文章链接.
VS-C# 系列:所有相关C#文章链接.
bat 系列:所有相关bat文章链接.
OpenCV 系列:所有相关OpenCV文章链接.


Keil 系列:所有相关文章链接
所有内容均以最小系统调试成功;逐步提供低分源码工程下载
保证每行代码都经过验证!
如有疑惑,欢迎留言,看见即回;祝好__by Dxg_LC

序言:
1、以上链接为方便整理查看资料用;伴随博文发布更新,如果有不正确处,感谢指正
2、因本人能力有限若有不正确之处或者相关超链接失效,请于相关文章内提醒@博主;灰常感谢
3、友情提醒1,勿要《一支烟 + 一杯茶 == 一坐一下午》 身体重要,革命本钱;
4、友情提醒2,多喝热水;
5、友情提醒3,听媳妇话+多点时间陪家人;
在这里插入图片描述

1、C# Dictionary键值对 创建,删除,清空

using System.Collections.Generic;
//创建
Dictionary<string, int> dictionary = new Dictionary<string, int>();
//增加
dictionary.add("1",1);
dictionary.add("2",2);
dictionary.add("3",3);
dictionary.add("4",4);
//删除
dictionary.Remove("2",2);
//清空
dictionary.Clear();

Dxg-原创出品,如需转载,请注明出处;

欢迎收藏,点赞;"一键三联"走起,LOL

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
C#中,可以使用异步方法来处理键值对。具体实现可以使用异步的Dictionary类,它提供了一些异步的方法来访问和操作键值对。例如,可以使用异步的AddAsync方法来向字典中添加键值对,使用异步的ContainsKeyAsync方法来检查是否存在某个键等等。以下是一个使用异步方法处理键值对的示例代码: ```csharp using System.Collections.Generic; using System.Threading.Tasks; class Program { static Dictionary<string, string> dictionary = new Dictionary<string, string>(); static async Task Main(string[] args) { await dictionary.AddAsync("key1", "value1"); bool containsKey = await dictionary.ContainsKeyAsync("key1"); string value = await dictionary.GetValueAsync("key1"); await dictionary.RemoveAsync("key1"); } } public static class DictionaryExtensions { public static async Task AddAsync<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value) { await Task.Run(() => dictionary.Add(key, value)); } public static async Task<bool> ContainsKeyAsync<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key) { return await Task.Run(() => dictionary.ContainsKey(key)); } public static async Task<TValue> GetValueAsync<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key) { return await Task.Run(() => dictionary[key]); } public static async Task RemoveAsync<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key) { await Task.Run(() => dictionary.Remove(key)); } } ``` 在这个示例中,我们使用了异步方法AddAsync、ContainsKeyAsync、GetValueAsync和RemoveAsync来向字典中添加、检查、获取和删除键值对。这些方法都是异步的,可以在异步环境下使用,比如在异步方法或线程池任务中。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

淘气坏坏besos

原创干货分析,欢迎大佬打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值