Dictionary泛型集合
文章目录
- 1 Dictionary
1 Dictionary<K, V>
1.1 Dictionary<K, V>简介
关于Dictionary<K, V>泛型集合:
- Dictionary<K, V>通常称为字典,<K, V>约束集合中元素类型。
- 编译时检查约束类型,无需装箱拆箱操作,与哈希表操作类似。
Dictionary<K, V>的存储结构:
1.2 Dictionary<K, V>的创建
- 使用Add添加:
//使用Add方法添加
Dictionary<string, Student> stuDic1 = new Dictionary<string, Student>();
stuDic1.Add("VIP1", student1);
stuDic1.Add("VIP2", student2);
stuDic1.Add("VIP3", student3);
stuDic1.Add("VIP4", student4);
stuDic1.Add("VIP5", student5);
- 使用集合初始化器:
//使用集合初始化器
Dictionary<string, Student> stuDic2 = new Dictionary<string, Student>()
{["VIP1"]=student1,["VIP2"] = student2,["VIP3"] = student3,["VIP4"] = student4,["VIP5"] = student5,
};
- 集合的嵌套:
//集合的嵌套(比如:1班 5个学生成绩 2 班有5个学员成绩....)
List<int> class1List = new List<int> { 90, 80, 60, 79, 82 };
List<int> class2List = new List<int> { 93, 85, 60, 79, 82 };
List<int> class3List = new List<int> { 92, 80, 60, 89, 88 };Dictionary<string, List<int>> classList = new Dictionary<string, List<int>>()
{["软件1班"]= class1List,["软件2班"] = class2List,["软件3班"] = class3List
};
1.3 Dictionary<K, V>的访问和遍历
- 通过key访问value:
//通过key访问value
Student student = stuDic1["VIP3"];
Console.WriteLine(student.StudentName);
- 遍历key:
//遍历集合keys
foreach (string key in stuDic1.Keys){Console.WriteLine(key);}
- 遍历values:
//遍历集合values
foreach (Student item in stuDic1.Values)
{Console.WriteLine(item.StudentId+"\t" + item.StudentName + "\t" + item.Age);
}
参考资料:
- .NET/C#工控上位机VIP系统学习班【喜科堂互联教育】
Dictionary泛型集合相关推荐
- C#中的泛型和泛型集合
泛型 泛型引入了一个概念:类型参数.通过使用类型参数(T)减少了运行时强制转换或装箱操作的风险,通过泛型可以最大限度的重用代码,保护类型的安全及提高性能,他的最常见应用就是创建集合类,可以约束集合类中 ...
- C#泛型集合之Dictionaryk, v使用技巧
1.要使用Dictionary集合,需要导入C#泛型命名空间 System.Collections.Generic(程序集:mscorlib) 2.描述 1).从一组键(Key)到一组值(Value) ...
- C#泛型-泛型集合DictionaryK,V
在System.Collections.Generic命名空间中,与ArrayList相对应的泛型集合是List<T>,与 HashTable相对应的泛型集合是Dictionary< ...
- C#中的集合、哈希表、泛型集合、字典
集合ArrayList 集合:集合可以看做是数组,可以将其看成"长度可变,具有很多方法的数组".使用ArrayList,首先需要导入命名空间using.system.collect ...
- 集合与泛型集合与键值对集合
1,集合 (Connections) ArrayList arr = new ArrayList();//可以add arr.Add("Hello girls!");//支持添加o ...
- 泛型集合 无序泛型 c#
导入命名空间 using System.Collections.Generic; 定义泛型集合 无序 // Dictionary<键类型,值类型> 变量名 = new Dictionary ...
- C#学习七(包含File字节流,list泛型集合、拆装箱、ictionary字典,文件流FileStream、StreamReader和StreamWrite、多态)
一.对前面部份的总结 using System.Collections; using System.IO; using System.Text;namespace _074_复习 {class Pro ...
- 员工打卡案列使用泛型集合的解决方法以及一些遇到的问题(2018年4月4号突如其来的雪花纷飞)...
1.需要用到的窗体和类 主窗体代码 using System; using System.Collections.Generic; using System.ComponentModel; using ...
- C#泛型集合定义及使用
C# 语言中泛型集合是泛型中最常见的应用,主要用于约束集合中存放的元素. 由于在集合中能存放任意类型的值,在取值时经常会遇到数据类型转换异常的情况,因此推荐在定义集合时使用泛型集合. 前面<C# ...
最新文章
- 【python之旅】python的基础二
- java如何使用elasticsearch
- shell 全局和局部变量
- DHTML【2】--HTML
- SAP Fiori里两种锁机制(lock)的实现
- C#------如何判断输入的是否为纯数字
- vue项目中assets与static的区别
- 如何改变标题栏的宽度
- java 输出中文_没见过的 Java 编程入门教程!例程使用中文标识符代码:问个好吧...
- TCP 三次握手和四次挥手个人理解
- 【深度学习论文】:U-Net
- IT兄弟连 JavaWeb教程 Servlet会话跟踪 Session优缺点
- 获取本机的ip地址(Windows和linux的区别)
- ora 01033 oracle initialization,ora-01033:oracle initialization or shutdown in progress 解决方法
- (转)Y480 完美黑苹果全套驱动分享
- 使用FTP进行主机与Linux的文件传输
- 【Python】Pandas GroupBy 深度总结
- 还在犹豫写不写博客吗?
- 伪随机生成器具体实现——杂乱的方法
- 1.Hadoop入门
热门文章
- mysql 安装手册(转)
- 1、C语言面试笔试---变量定义和声明
- 自建DNS服务器,了解一代防火墙必备网络环境,局域网设备域名访问教程
- 【任务脚本】0528京东618叠蛋糕任务脚本全自动脚本,京东任务全自动程序稳定运行,向大神致敬...
- 【Android工具】最新测试谷歌play耗电情况,各种品牌安装谷歌play方法,GooglePlay...
- 12.流水线设计方式
- 聊聊flink的Tumbling Window
- Gradle 之 Android 中的应用
- css选择器 nth-child
- 管理信息系统的开发和管理