C#泛型集合—Dictionary<TKey,TValue>使用方法
来源:互联网 发布:kele大淘客cms 编辑:程序博客网 时间:2024/06/05 15:10
首先要使用Dictionary集合,需要导入c#泛型命名空间
using System.Collections.Generic;
描述
任何键都必须是唯一的,且不能为空
若值为引用类型,则可以为空值
key和value可以是任何类型
创建及初始化
public Dictionary<string,int> mDictionary =new Dictionary<string,int>();
添加元素 Add(string key,int value)
mDictionary.Add("one",1);
mDictionary.Add("two",2);
mDictionary.Add("three",3);
mDictionary.Add("four",4);
集合元素个数 Count()
intmcount =mDictionary.Count();
查找元素 ContainsKey(string key)
if(mDictionary.ContainsKey("one"))
{
int num =mDictionary["one"];
}
删除元素Remove(string key)
mDictionary.Remove("two");
遍历元素
foreach (KeyValuePair<string,int> kvp in myDictionary)
{
string key = kvp.Key;
int value = kvp.Value;
}
仅遍历键属性
Dictionary<string,int>.KeyCollection keyCol = mDictionary.Keys;
foreach(string key in keyCol)
{
string strkey =key;
}
仅遍历值属性
Dictionary<string,int>.ValueCollection valueCol = mDictionary.Values;
foreach(string value invalueCol)
{
int ivalue = value;
}
using System.Collections.Generic;
描述
任何键都必须是唯一的,且不能为空
若值为引用类型,则可以为空值
key和value可以是任何类型
创建及初始化
public Dictionary<string,int> mDictionary =new Dictionary<string,int>();
添加元素 Add(string key,int value)
mDictionary.Add("one",1);
mDictionary.Add("two",2);
mDictionary.Add("three",3);
mDictionary.Add("four",4);
集合元素个数 Count()
intmcount =mDictionary.Count();
查找元素 ContainsKey(string key)
if(mDictionary.ContainsKey("one"))
{
int num =mDictionary["one"];
}
删除元素Remove(string key)
mDictionary.Remove("two");
遍历元素
foreach (KeyValuePair<string,int> kvp in myDictionary)
{
string key = kvp.Key;
int value = kvp.Value;
}
仅遍历键属性
Dictionary<string,int>.KeyCollection keyCol = mDictionary.Keys;
foreach(string key in keyCol)
{
string strkey =key;
}
仅遍历值属性
Dictionary<string,int>.ValueCollection valueCol = mDictionary.Values;
foreach(string value invalueCol)
{
int ivalue = value;
}
- C#泛型集合—Dictionary<TKey,TValue>使用方法
- c# 泛型集合Dictionary<TKey,TValue>
- c# 泛型集合Dictionary<TKey,TValue>
- C#中Dictionary<Tkey,Tvalue>泛型集合漫谈
- Dictionary<TKey, TValue>集合类
- Dictionary<TKey, TValue> 散列集合
- [C#]泛型与非泛型集合类的区别及使用例程,包括ArrayList,Hashtable,List<T>,Dictionary<Tkey,Tvalue>,SortedList<Tkey,Tvalue>,
- 泛型与非泛型集合类的区别及使用例程,包括ArrayList,Hashtable,List<T>,Dictionary<Tkey,Tvalue>,SortedList<Tkey,Tvalue>,Queu
- C# Dictionary<TKey, TValue>的学习
- C#中Dictionary<TKey,TValue>排序方式
- C# Dictionary的用法 KeyValuePair<TKey, TValue> 结构
- C#高级编程五十三天----字典Dictionary<TKey,TValue>
- KeyValuePair<TKey, TValue>泛型用法
- C# - IOrderedDictionary<TKey, TValue> Implementation
- C# - OrderedDictionary<TKey, TValue> Implementation
- C# SortedDictionary<TKey, TValue> 类
- Dictionary<TKey, TValue> 的foreach便利方法
- .net集合类的研究-哈希表(一)--Hashtable,Dictionary<TKey,TValue>
- 一个shell脚本执行出错的问题
- 2013移动APP界面设计趋势与设计理念
- putty 编译问题
- 关于浏览器状态栏不能隐藏的设置
- MS SQL 显示进程执行的脚本
- C#泛型集合—Dictionary<TKey,TValue>使用方法
- Adaboost理解:人脸检测
- 深度神经网络学习资料整理
- Convert Sorted Array/list to Binary Search Tree
- Linux使用dd命令测试硬盘读写速度
- Linux LVM学习
- 浅谈工作中使用过的几种C++界面库
- DNS查询
- 线程注意事项