C# 之 Dictionary 详解

https://blog.csdn.net/dmlk31/article/details/111206272

.说明

1、必须包含名空间 System.Collection.Generic
2、Dictionary里面的每一个元素都是一个键值对(由二个元素组成:键和值)
3、键必须是唯一的,而值不需要唯一的
4、键和值都可以是任何类型(比如:string, int, 自定义类型等等)
5、可以简单将 Dictionary 理解为 键值对 数据的集合

Dictionary能提供快速的基于键值的元素查找。

▪ 常规使用方法

// 定义
Dictionary<string, string> dictExecutes = new Dictionary<string, string>();

// 添加元素
dictExecutes.Add("bmp", "paint.exe");
dictExecutes.Add("dib", "paint.exe");
dictExecutes.Add("rtf", "wordpad.exe");
dictExecutes.Add("txt", "notepad.exe");

// 取值
Console.WriteLine("For key = 'rtf', value = {0}.", dictExecutes["rtf"]);

// 改值
dictExecutes["rtf"] = "winword.exe";
Console.WriteLine("For key = 'rtf', value = {0}.", dictExecutes["rtf"]);

// 遍历 KEY
foreach( string key in dictExecutes.Keys ) Console.WriteLine("Key = {0}", key);

// 遍历 VALUE
foreach( string value in dictExecutes.Values ) Console.WriteLine("value = {0}", value);

// 遍历字典
foreach( KeyValuePair<string, string> kvp in dictExecutes ) Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);

// 添加存在的元素
try{
dictExecutes.Add("txt", "winword.exe");
}catch( ArgumentException ){
Console.WriteLine("An element with Key = 'txt' already exists.");
}

// 删除元素
dictExecutes.Remove("doc");
if( !dictExecutes.ContainsKey("doc") ) Console.WriteLine("Key 'doc' is not found.");

// 判断键存在
if( openWith.ContainsKey("bmp") ) Console.WriteLine("An element with Key = 'bmp' exists.");

▪ 参数为其它类型
// 参数为其它类型
Dictionary<int, string[]> dictOthers = new Dictionary<int, string[]>();

dictOthers.Add(1, "1,11,111".Split(','));
dictOthers.Add(2, "2,22,222".Split(','));

Console.WriteLine(dictOthers[1][2]);

▪ 参数为自定义类型
// 首先定义类
class DouCube
{
private int _Code;
public int Code { get{ return _Code; } set{ _Code = value; } }

private string _Page;
public string Page { get{ return _Page; } set{ _Page = value; } }
}

// 声明并添加元素
Dictionary<int, DouCube> MyTypes = new Dictionary<int, DouCube>();

for( int i = 1; i <= 9; i++ ){
DouCube elem = new DouCube();

elem.Code = i * 100;
elem.Page = "http://www.doucube.com/" + i.ToString() + ".html";

MyTypes.Add(i, elem);
}

// 遍历元素
foreach( KeyValuePair<int, DouCube> kvp in MyTypes ){
Console.WriteLine("Index {0} Code:{1} Page:{2}", kvp.Key, kvp.Value.Code, kvp.Value.Page);
}

其它常见属性和方法的说明:

 

  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:      获取与指定的键相关联的值。

 

通过某一个一定的[key]去找到对应的值


实例化; Dictionary<string,int> dit =new Dictionary<string,int >

dit.Add("小明",9);

int t=dit["小明"]

例子:

输出最大值的名字

var keys =dit.OrderByDescending(t=>t.Value).Select(q => q.Key);

List<string> keyList=keys.ToList<string>();
Console.WriteLine(keyList[0])

 

var list=dic.Keys.ToList() //dictionsry转为list

————————————————

版权声明:本文为CSDN博主「外来物种」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/dmlk31/article/details/111206272

posted @   yinghualeihenmei  阅读(593)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示