Dictionary有Key跟Value,一个Key只能对应一种Value,但一种Value可以对应很多种Key
像是学生座号和成绩,每个人只会对应到一个成绩,但也许有人同时考到相同的分数

下面是官方的范例

using System;
using System.Collections.Generic;public class Example
{public static void Main(){// Create a new dictionary of strings, with string keys.//Dictionary<string, string> openWith = new Dictionary<string, string>();// Add some elements to the dictionary. There are no // duplicate keys, but some of the values are duplicates.openWith.Add("txt", "notepad.exe");openWith.Add("bmp", "paint.exe");openWith.Add("dib", "paint.exe");openWith.Add("rtf", "wordpad.exe");// The Add method throws an exception if the new key is // already in the dictionary.try{openWith.Add("txt", "winword.exe");}catch (ArgumentException){Console.WriteLine("An element with Key = \"txt\" already exists.");}#output => An element with Key = "txt" already exists.*/

看不懂的话 可以参考这篇
http://code2study.blogspot.com/2012/01/c-dictionary.html


Dictionary<string, string> MyDic = new Dictionary<string, string>( );// 建立字典
private void CreateDictionary( )
{MyDic.Add( "Name", "Jack" );MyDic.Add( "Blog", "Jack’s Blog" );MyDic.Add( "Group", "KTV Group" );
}// 查字典
private String FindInDictionary( String FindMe )
{if ( true == ( MyDic.ContainsKey( FindMe ) ) ){return MyDic[ FindMe ];}else{return "Not Found";}
}// 巡整個字典
private void ShowAllInDictionary( )
{foreach ( var OneItem in MyDic ){Console.WriteLine( "Key = " + OneItem.Key + ", Value = " + OneItem.Value );}
}

除了上面那種先宣告再Add的方式之外,也可以用底下的方式來直接宣告並產生內容

Dictionary<string, string> dctNewWay =new Dictionary<string, string>(){{"Key1", "AAAA"}, {"Key2", "BBBB"},{"Key3", "CCCC"}, {"Key4", "DDDD"}};

另外,值得一提的是,Key跟Value並不限定只能用String,什麼型別都可以,所以你也可以把Class塞到Value,然後其中一個屬性拿出來當作Key,就可以方便的確認有沒有重複的Key,不用塞到SQL才知道。

C# Dictionary相关推荐

  1. vim 高级应用 原文地址 http://www.2maomao.com/blog/wp-content/uploads/vim_tips.txt

    最佳vim技巧 ---------------------------------------- # 信息来源 ---------------------------------------- www ...

  2. Python 字典(Dictionary) get()方法

    Python 字典(Dictionary) get()方法 描述 Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 get()方法语法: ...

  3. Python 字典(Dictionary)

    Python 字典(Dictionary) 字典是另一种可变容器模型,且可存储任意类型对象. 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在 ...

  4. Dictionary作为数据源绑定,调用c++库中返回为BYTE*的函数,listView项排序

    最近在做一个电子档案管理的项目.现在还处于初期,只是做一个简单demo拿去跟客户演示.至于最后谈不谈得下来,到底做不做,反正我是不看好,但没因为这样就马马虎虎.草草了事.这个项目算是b/s加c/s混合 ...

  5. 技术图文:浅析 C# Dictionary实现原理

    背景 对于 C# 中的 Dictionary类 相信大家都不陌生,这是一个 Collection(集合) 类型,可以通过 Key/Value (键值对) 的形式来存放数据:该类最大的优点就是它查找元素 ...

  6. python报错 ValueError: dictionary update sequence element #0 has length 1; 2 is require

    原文链接地址: https://blog.csdn.net/weixin_40894428/article/details/80683137 字符串转字典要用eval(),这个方法很多书上都没有介绍, ...

  7. 迭代var()内置函数的时候出现RuntimeError: dictionary changed size during iteration的解决办法...

    下午看了Mr Seven的教学视频,其中有一段讲全局变量的视频,迭代输出全局变量的时候报错了. 视频中的做法: for k,v in vars().items():print(k) 打印结果 for ...

  8. C#中的Dictionary简介

    C#中的Dictionary简介  http://www.cnblogs.com/ccczqh/archive/2011/01/04/1925852.html 简介 在C#中,Dictionary提供 ...

  9. python 读取文件到字典读取顺序_python_实现dictionary按照输入顺序输出

    背景: 需要读取一个csv文件,并将其数据保存在dictionary中,并按照读文件的顺序输出,且不能引用第三方包,只能使用python自带模块.输出格式如下: 在使用python的dictionar ...

  10. python 字典代码_Python 字典(Dictionary)

    Python 字典(Dictionary) 字典是另一种可变容器模型,且可存储任意类型对象. 字典的每个键值 key=>value 对用冒号 : 分割,每个键值对之间用逗号 , 分割,整个字典包 ...

最新文章

  1. java mysql failover_MySQL MMM 双主在Failover时挂起
  2. 解决python2和python3的pip冲突
  3. QEMU KVM Libvirt手册(7): 硬件虚拟化
  4. oracle bcp out,SQL Server利用bcp命令把SQL语句结果生成文本文件
  5. 辗转相除求最大公约数最小公倍数 扩展欧几里得算法
  6. bzoj 1665: [Usaco2006 Open]The Climbing Wall 攀岩(最短路)
  7. python openpyxl合并单元格_用openpyxl分析xlsx文件的合并单元格
  8. ReocketMq常用命令
  9. python变量未定义_引入模块的全局变量未定义
  10. 32位有符号整数_(Trivial)LeetCode 8—字符串转化为整数(atoi)
  11. Python下多变量联合分布图(pairplot)绘制——seaborn
  12. 中国各省名称+ 经纬度
  13. 计算机标准用户英文名称,标准计算机专业英文简历范文
  14. 永中文档在线预览集群部署方案
  15. 分享10个最佳Linux VPS服务器托管
  16. 中国版Second Life前途难料
  17. 新能源车辆越来越多了,车牌识别核心技术
  18. VS关闭vue语法检测
  19. 牛客网网易机试模拟题(Android)记录
  20. 计算机c盘可以删除的文件,电脑C盘上的文件哪些可以大胆删除的?

热门文章

  1. 使用ADB连接机顶盒并抓取设备操作日志
  2. excel到期弹窗提醒桌面弹_excel中某列日期到期提前弹窗提醒()
  3. 关于VMware 系统重装后 注册码无效的问题
  4. 基于scrapy的B站UP主信息爬取
  5. Python抽取豆瓣电影信息
  6. 【python 数据框apply】数据框多列计算调用apply函数
  7. 不再迷惑,也许之前你从未真正懂得 Scroller 及滑动机制
  8. 大声说出我爱你—英语发音学习总结
  9. 九、Client测试技术
  10. background 部分用法 背景颜色渐变