首页 > 编程语言 >C# 之 Dictionary 详解

C# 之 Dictionary 详解

时间:2022-11-13 21:45:35浏览次数:39  
标签:dictExecutes Code Console Dictionary C# Add 详解 WriteLine

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);
}
————————————————

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

标签:dictExecutes,Code,Console,Dictionary,C#,Add,详解,WriteLine
From: https://www.cnblogs.com/Dongmy/p/16884297.html

相关文章

  • Docker部署Redis
    1.拉取redis镜像dockerpullredis:latest2.运行docker容器     dockerrun-p6379:6379 \    -v/mydata/redis/data:/data \     -v/mydat......
  • Docker部署Nginx
    docker安装nginx1.下载nginx镜像dockerpullnginx2.创建nginx挂载目录    mkdir-p/usr/local/nginx/{conf,html,log,ssl}3.启动nginx容器,用于copy一些文件放......
  • The 2021 ICPC Asia Shenyang Regional Contest B
    B.BitwiseExclusive-ORSequence我们考虑一种构造方式就是让一个点为0然后连接他的就一直异或上他连接的目标这样虽然不能符合题意达到最小但是我们可以发现这个如......
  • Docker部署RabbitMQ
    1.获取RabbitMQ最新镜像dockerpullrabbitmq:latest 2.创建rabbitmq相关挂载目录mkdir-p/usr/local/rabbitmq/{data,conf,log} 3.创建完成之后要对所创建文件授......
  • 75.Sort Colors
    Givenanarraywith n objectscoloredred,whiteorblue,sortthemsothatobjectsofthesamecolorareadjacent,withthecolorsintheorderred,white......
  • C#通过其他类更新textbox控件
    应用需求:经常我们会碰到这样的情况,我们需要在其他C#类里面调用控件并修改其中的某个属性。解决办法:1.在控件类中添加静态变量:publicstaticForm1form;12.然后在其构造......
  • 《Design by Contract for Embedded Software》 翻译
    DesignbyContract isthesinglemosteffectiveprogrammingtechniquefordeliveringhigh-qualitycode.HereyoucanlearnwhattheDesignbyContractprogram......
  • sqlalchemy的连接方式
    这是比较推荐的连接方式,基于threading.local实现的。#-*-coding:utf-8-*-fromsqlalchemyimportcreate_enginefromsqlalchemy.ormimportsessionmaker,sc......
  • docker部署redis
    1、问题描述docker部署redis,记录下。2、问题说明2.1、搜索redis,可跳过dockersearchredis2.2获取docker镜像dockerpullredis2.3构建容器前准备工作,挂载文件......
  • ERC721开发NFT--接口
    使用https://docs.openzeppelin.com/contracts/4.x/erc721中的简单案例pragmasolidity^0.8.0;import"@openzeppelin/contracts/token/ERC721/extensions/ERC721U......