C#中Dictionary泛型集合7种常见的用法
作者:jae 时间:2021-08-07 04:47:17
要使用Dictionary集合,需要导入C#泛型命名空间
System.Collections.Generic(程序集:mscorlib)
Dictionary的描述
1、从一组键(Key)到一组值(Value)的映射,每一个添加项都是由一个值及其相关连的键组成
2、任何键都必须是唯一的
3、键不能为空引用null(VB中的Nothing),若值为引用类型,则可以为空值
4、Key和Value可以是任何类型(string,int,custom class 等)
Dictionary常用用法:以 key 的类型为 int , value的类型为string 为例
1、创建及初始化
Dictionary<int,string>myDictionary=newDictionary<int,string>();
2、添加元素
myDictionary.Add(1,"C#");
myDictionary.Add(2,"C++");
myDictionary.Add(3,"ASP.NET");
myDictionary.Add(4,"MVC");
3、通过Key查找元素
if(myDictionary.ContainsKey(1))
{
Console.WriteLine("Key:{0},Value:{1}","1", myDictionary[1]);
}
4、通过KeyValuePair遍历元素
foreach(KeyValuePair<int,string>kvp in myDictionary)
{
Console.WriteLine("Key = {0}, Value = {1}",kvp.Key, kvp.Value);
}
5、仅遍历键 Keys 属性
Dictionary<int,string>.KeyCollection keyCol=myDictionary.Keys;
foreach(intkeyinkeyCol)
{
Console.WriteLine("Key = {0}", key);
}
6、仅遍历值 Valus属性
Dictionary<int,string>.ValueCollection valueCol=myDictionary.Values;
foreach(stringvalueinvalueCol)
{
Console.WriteLine("Value = {0}", value);
}
7、通过Remove方法移除指定的键值
myDictionary.Remove(1);
if(myDictionary.ContainsKey(1))
{
Console.WriteLine("Key:{0},Value:{1}","1", myDictionary[1]);
}
else
{
Console.WriteLine("不存在 Key : 1");
}
其它常见属性和方法的说明:
Comparer: | 获取用于确定字典中的键是否相等的 IEqualityComparer。 |
Count: | 获取包含在 Dictionary中的键/值对的数目。 |
Item: | 获取或设置与指定的键相关联的值。 |
Keys: | 获取包含 Dictionary中的键的集合。 |
Values: | 获取包含 Dictionary中的值的集合。 |
Add: | 将指定的键和值添加到字典中。 |
Clear: | 从 Dictionary中移除所有的键和值。 |
ContainsKey: | 确定 Dictionary是否包含指定的键。 |
ContainsValue: | 确定 Dictionary是否包含特定值。 |
GetEnumerator: | 返回循环访问 Dictionary的枚举数。 |
GetType: | 获取当前实例的 Type。 (从 Object 继承。) |
Remove: | 从 Dictionary中移除所指定的键的值。 |
ToString: | 返回表示当前 Object的 String。 (从 Object 继承。) |
TryGetValue: | 获取与指定的键相关联的值。 |
标签:C#,Dictionary,泛型,集合
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
深入多线程之:用Wait与Pulse模拟一些同步构造的应用详解
2021-07-09 06:14:19
![](https://img.aspxhome.com/file/2023/2/96312_0s.png)
Java使用Scanner类进行控制台输入实现方法
2021-08-08 18:34:26
Java 中的字符串常量池详解
2023-08-15 04:43:45
图解Java经典算法冒泡排序的原理与实现
2023-03-14 21:41:23
![](https://img.aspxhome.com/file/2023/0/71020_0s.gif)
C#中的那些警告该如何去除(完全去除C#警告)
2023-09-07 20:55:53
![](https://img.aspxhome.com/file/2023/1/76381_0s.png)
Java判断字符串是否为IP地址的方法
2023-07-06 15:02:58
一文详解Spring如何控制Bean注入的顺序
2022-09-10 18:33:22
![](https://img.aspxhome.com/file/2023/7/70947_0s.jpg)
Eclipse项目怎么导入IDEA并运行(超详细)
2021-11-15 06:51:41
![](https://img.aspxhome.com/file/2023/1/97811_0s.jpg)
详解升级Android Studio3.0时遇到的几个问题
2021-11-19 08:13:52
Java实现读取resources目录下的文件路径的九种方式
2023-11-23 11:47:02
C#使用NPOI实现Excel和DataTable的互转
2022-12-06 22:51:05
![](https://img.aspxhome.com/file/2023/6/87026_0s.jpg)
Androd 勇闯高阶性能优化之布局优化篇
2023-01-22 12:12:15
![](https://img.aspxhome.com/file/2023/8/87278_0s.png)
Java反射机制的简单讲解
2023-12-12 20:49:13
![](https://img.aspxhome.com/file/2023/6/64796_0s.png)
AQS(AbstractQueuedSynchronizer)抽象队列同步器及工作原理解析
2023-02-24 22:20:09
![](https://img.aspxhome.com/file/2023/6/84216_0s.png)
java计算工作时间除去节假日以及双休日
2023-09-24 20:34:58
![](https://img.aspxhome.com/file/2023/5/71235_0s.jpg)
Java基础详解之内存泄漏
2022-04-14 03:45:46
深入理解 Java、Kotlin、Go 的线程和协程
2022-05-04 12:39:14
![](https://img.aspxhome.com/file/2023/8/70288_0s.jpg)
JAVA 实现二叉树(链式存储结构)
2022-02-18 11:35:21
![](https://img.aspxhome.com/file/2023/4/83024_0s.jpg)
使用Feign扩展包实现微服务间文件上传
2023-04-28 01:04:31
![](https://img.aspxhome.com/file/2023/5/63085_0s.jpg)
Java 从Set里面取出有序的记录详解及实例
2021-07-08 09:07:33