C#中的Dictionary的使用
在工作中有时候会用到Dictionary,由于自己之前没用过,参考了一下前人和先辈的实践,仿照着写了一个Test,第一次用还不是很熟练,要多实践练习才能用的得心应手,写代码重在敲键盘,以此为诫。(主要注意下这个Dictionary的key和value以及添加数据方式)
写了三个方法和一个Student类。
namespace DictionaryTest
{
class Program
{
static void Main(string[] args)
{
DicSample1();
DicSample2();
DicSample3();
Console.ReadLine();
} public static void DicSample1()
{
Dictionary<string, string> dic = new Dictionary<string, string>();
if (dic.ContainsKey("Item1") == false)
{
dic.Add("Item1", "ZheJiang");
}
if (dic.ContainsKey("Item2") == false)
{
dic.Add("Item2", "ShangHai");
}
else
{
dic["Item2"] = "ShangHai";
}
if (dic.ContainsKey("Item3") == false)
{
dic.Add("Item3", "BeiJing");
}
if (dic.ContainsKey("Item1"))
{
Console.WriteLine("Output:" + dic["Item1"]);
}
//遍历key
foreach (var key in dic.Keys)
{
Console.WriteLine("Output key :{0}" , dic.Keys);
}
//遍历value
foreach (var value in dic.Values)
{
Console.WriteLine("Output value :{0}", dic.Values);
} foreach (var d in dic)
{
Console.WriteLine("Output key:{0}, value:{1}", d.Key, d.Value);
}
} public static void DicSample2()
{
Dictionary<string, string[]> dics = new Dictionary<string, string[]>();
string[] ZheJiang = { "Hangzhou", "Huzhou", "Taizhou", "Wenzhou", "Quzhou" };
string[] AnHui = { "Hefei", "Wuhu", "Huainan", "Fuyang", "Huangshan" };
dics.Add("ZJ", ZheJiang);
dics.Add("AH", AnHui);
for (int i = ; i <= ZheJiang.Count() - ; i++)
{ Console.WriteLine("Output:" + dics["ZJ"][i]); }
for (int j = ; j < AnHui.Count() - ; j++)
{ Console.WriteLine("Output:" + dics["AH"][j]); }
} public static void DicSample3()
{
Dictionary<string, Student> dics = new Dictionary<string, Student>();
Student student = null;
for (int i = ; i < ; i++)
{
student = new Student();
student.Num = i.ToString();
student.Name = "Student Name" + i.ToString();
dics.Add(i.ToString(), student);
}
foreach (var stuItem in dics)
{
Console.WriteLine("Output: key{0},Num{1},Name{2}", stuItem.Key, stuItem.Value.Num, stuItem.Value.Name);
}
}
}
public class Student
{
public string Name { get; set; }
public string Num { get; set; }
}
}
以下是运行结果:
6K]9U6C.jpg)
C#中的Dictionary的使用的更多相关文章
- JavaScript基础精华03(String对象,Array对象,循环遍历数组,JS中的Dictionary,Array的简化声明)
String对象(*) length属性:获取字符串的字符个数.(无论中文字符还是英文字符都算1个字符.) charAt(index)方法:获取指定索引位置的字符.(索引从0开始) indexOf(‘ ...
- C#中的Dictionary简介
简介在C#中,Dictionary提供快速的基于兼职的元素查找.当你有很多元素的时候可以使用它.它包含在System.Collections.Generic名空间中. 在使用前,你必须声明它的键类型和 ...
- VB中的Dictionary对象
VB中的Dictionary对象 Dictionary对象不是VBA或Visual Basic实时语言的具体存在的部分,它是存在于Microsoft Scripting Runtime Library ...
- C#中使用Dictionary实现Map数据结构——VC编程网
转载自: http://blog.51cto.com/psnx168 在VC中使用过CMap以及在Java中使用过Map的朋友应该很熟悉,使用Map可以方便实现基于键值对数据的处理,在C#中,你就需要 ...
- [转] C#中的Dictionary的使用
txw1958 的 原文 说明 必须包含名空间System.Collection.Generic Dictionary里面的每一个元素都是一个键值对(由二个元素组成:键和值) 键 ...
- (转)C#中的Dictionary字典类介绍
关键字:C# Dictionary 字典 作者:txw1958原文:http://www.cnblogs.com/txw1958/archive/2012/11/07/csharp-dictionar ...
- C#中的Dictionary字典类介绍
Dictionary字典类介绍 必须包含名空间System.Collection.Generic Dictionary里面的每一个元素都是一个键值对(由二个元素组成:键和值) 键必须是 ...
- C# .Net 中字典Dictionary<TKey,TValue>泛型类 学习浅谈
一.综述: Dictionary<TKey,TValue>是在 .NET Framework 2.0 版中是新增的.表示键值对的集合,Dictionary<TKey,TValue&g ...
- C#中的Dictionary类,默认key是区分大小写的
在C#中定义一个Dictionary Dictionary<string,string> dictionary = new Dictionary<string,string>( ...
- Python中的Dictionary
Dictionary的创建 1 字面量 >>>D = {'a': 1, 'b': 2} >>>D {'b': 2, 'a': 1} 2 keyword参数 > ...
随机推荐
- Oracle与mysql的字段类型整理
Oralce的字段类型整理如下: Mysql的字段类型整理如下: 最后面一栏是对应JAVA的基本类型.希望对初学者有用,初学者在学习JAVA的时候,不知道怎么把JAVA的对象指向到ORALCE或者MY ...
- sscanf( )函数初体验
解析字符串,将%格式的内容,存储到后面的参数中 %% - 返回一个百分号 % %c - ASCII 值对应的字符 %d - 包含正负号的十进制数(负数.0.正数) %e - 使用小写的科学计数法(例如 ...
- [刷题]算法竞赛入门经典(第2版) 5-8/UVa230 - Borrowers
//又开学啦,不知不觉成为大二的老人了...时间过得好快啊,感觉好颓废... 题意:建立一个借书/归还系统.有借.还.把还的书插到书架上这三个指令. 代码:(Accepted, 0ms) //UVa2 ...
- Python学习(一) —— matplotlib绘制三维轨迹图
在研究SLAM时常常需要对其输出的位姿进行复现以检测算法效果,在ubuntu系统中使用Python可以很好的完成相关的工作. 一. Ubuntu下Python的使用 在Ubuntu下使用Python有 ...
- .NET面试题系列[17] - 多线程概念(2)
线程概念 线程和进程的区别 进程是应用程序的一个实例要使用的资源的一个集合.进程通过虚拟内存地址空间进行隔离,确保各个进程之间不会相互影响.同一个进程中的各个线程之间共享进程拥有的所有资源. 线程是系 ...
- 又是一个MVP+RxJava+Retrofit的干货集中营
欢迎访问我的个人博客 ,未经允许不得转发 前言 今天想要与大家一起分享的是近一个星期内开发的一个新app--干货集中营客户端,其实网上已经有许多类似的项目,代码家也在他的干货集中营中推荐了几款优秀的作 ...
- 深入理解Struts2----数据校验
在表现层的数据处理方面主要分为两种类型,一种是类型转换,这点我们上篇已经简单介绍过,另外一种则是我们本篇文章将要介绍的:数据校验.对于我们的web应用,我们经常需要和用户进行交互收集用户信息,那么无论 ...
- Lists, Maps and Sets in Java
ArrayList vs LinkedList vs Vector From the hierarchy diagram, they all implement List interface. The ...
- html学习笔记 - 特殊字符
- 第 12 章 MySQL 可扩展设计的基本原则
前言: 随着信息量的飞速增加,硬件设备的发展已经慢慢的无法跟上应用系统对处理能力的要求了.此时,我们如何来解决系统对性能的要求?只有一个办法,那就是通过改造系统的架构体系,提升系统的扩展能力,通过组合 ...