C#中Dictionary的用法
在C#中,Dictionary提供快速的基于兼职的元素查找。他的结构是这样的:Dictionary<[key], [value]> ,当你有很多元素的时候可以使用它。它包含在System.Collections.Generic名空间中。在使用前,你必须声明它的键类型和值类型。
方法/步骤
要使用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");
}
- 4
其它常见属性和方法的说明:
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: 获取与指定的键相关联的值。
转载于:https://www.cnblogs.com/lxboy2009/p/3779460.html
C#中Dictionary的用法相关推荐
- python items函数用法,Python中dictionary items()系列函数的用法实例
本文实例讲述了Python中dictionary items()系列函数的用法,对Python程序设计有很好的参考借鉴价值.具体分析如下: 先来看一个示例: import html # availab ...
- python代码大全表解释-python中的字典用法大全的代码
如下代码是关于python中的字典用法大全的代码. #!/usr/bin/env python # # [SNIPPET_NAME: Dictionaries 101] # [SNIPPET_CATE ...
- python中print的用法_Python2与Python3中print用法总结
Python2中的print用法 在Python2 中 print 是一种输出语句 strHello = 'Hello Python' print strHello # Hello Python 1. ...
- java中demo接人_return的用法_如何理解java中return的用法?
C语言中return用法?(请熟练者进) return是返回值,这个返回值是和函数的类型有关的,函数的类型是什么,他的返回值就是什么 比方主函数intmain() {}这里就必须有一个return,只 ...
- Compound Words UVA - 10391(c++用法中substr函数用法+map实现)
题意: 给出字典中一堆单词,单词的输入方式是以字典序输入的.问:在这一堆单词中,有那些单词是通过其它两个单词组合而来的.按字典序升序输出这些单词. 题目: You are to find all th ...
- python代码大全-python中的字典用法大全的代码
如下代码是关于python中的字典用法大全的代码. #!/usr/bin/env python # # [SNIPPET_NAME: Dictionaries 101] # [SNIPPET_CATE ...
- gensim.corpora中Dictionaryd的用法
gensim.corpora中Dictionaryd的用法 调用 from gensim.corpora import Dictionary dictionary = Dictionary(corpu ...
- python中字典的使用_python中的字典用法大全
本文包含了python字典的各种用法,字典在python中的重要性不言而喻 #!/usr/bin/env python # # [代码名字: Dictionaries 101] # [代码分类: Py ...
- python 字典代码_python中的字典用法大全的代码
标签: 如下代码是关于python中的字典用法大全的代码. #!/usr/bin/env python # # [SNIPPET_NAME: Dictionaries 101] # [SNIPPET_ ...
最新文章
- Aliyun 挂载硬盘
- spring cloud 教程前言
- date设置时间_解决 IDEA 无法找到 java.util.Date 的问题
- ssh框架配置文件流程_通过配置文件和安排增强您的工作流程
- KLT(Kanade-Lucas-Tomasi )
- linux 权限管理 lvm,Linux LVM 设置和管理
- Java String到int,Java int到String
- Presto 日期和时间函数
- python中gm11_python实现灰色预测模型(GM11)——以预测股票收盘价为例
- Best practices for a new Go developer
- Win10磁盘管理器:轻松和安全地调整Win10的分区大小
- 软件测试概念-PIE模型
- 数据预处理 拉依达准则 matlab,数学建模数据预处理.doc
- 语音信号处理-概念(一):时域信号(横轴:时间;纵轴:幅值)、频谱图(横轴:频率;纵轴:幅值)--傅里叶变换-->时频谱图(语谱图/声谱图)【横轴:时间;纵轴:频率;颜色深浅:幅值】
- 【问题】不能加载 MSCOMCTL.OCX(机房问题)
- 空洞卷积原理详解及其pytorch代码实现
- c语言题模板大全,考试c语言题库
- 软件测试 -- 进阶 8 软件测试流程和过程
- 【开源电机驱动】H 桥驱动-软件篇
- HTML5系列代码:主色调红色的配色方案表