在工作中有时候会用到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; }
}
}

以下是运行结果:

C#中的Dictionary的使用的更多相关文章

  1. JavaScript基础精华03(String对象,Array对象,循环遍历数组,JS中的Dictionary,Array的简化声明)

    String对象(*) length属性:获取字符串的字符个数.(无论中文字符还是英文字符都算1个字符.) charAt(index)方法:获取指定索引位置的字符.(索引从0开始) indexOf(‘ ...

  2. C#中的Dictionary简介

    简介在C#中,Dictionary提供快速的基于兼职的元素查找.当你有很多元素的时候可以使用它.它包含在System.Collections.Generic名空间中. 在使用前,你必须声明它的键类型和 ...

  3. VB中的Dictionary对象

    VB中的Dictionary对象 Dictionary对象不是VBA或Visual Basic实时语言的具体存在的部分,它是存在于Microsoft Scripting Runtime Library ...

  4. C#中使用Dictionary实现Map数据结构——VC编程网

    转载自: http://blog.51cto.com/psnx168 在VC中使用过CMap以及在Java中使用过Map的朋友应该很熟悉,使用Map可以方便实现基于键值对数据的处理,在C#中,你就需要 ...

  5. [转] C#中的Dictionary的使用

    txw1958 的 原文 说明    必须包含名空间System.Collection.Generic     Dictionary里面的每一个元素都是一个键值对(由二个元素组成:键和值)     键 ...

  6. (转)C#中的Dictionary字典类介绍

    关键字:C# Dictionary 字典 作者:txw1958原文:http://www.cnblogs.com/txw1958/archive/2012/11/07/csharp-dictionar ...

  7. C#中的Dictionary字典类介绍

      Dictionary字典类介绍 必须包含名空间System.Collection.Generic    Dictionary里面的每一个元素都是一个键值对(由二个元素组成:键和值)    键必须是 ...

  8. C# .Net 中字典Dictionary<TKey,TValue>泛型类 学习浅谈

    一.综述: Dictionary<TKey,TValue>是在 .NET Framework 2.0 版中是新增的.表示键值对的集合,Dictionary<TKey,TValue&g ...

  9. C#中的Dictionary类,默认key是区分大小写的

    在C#中定义一个Dictionary Dictionary<string,string> dictionary = new Dictionary<string,string>( ...

  10. Python中的Dictionary

    Dictionary的创建 1 字面量 >>>D = {'a': 1, 'b': 2} >>>D {'b': 2, 'a': 1} 2 keyword参数 > ...

随机推荐

  1. libpng处理png图片(一)

    一:libpng库的编译 环境:windows10 + VS2013 需要下载:libpng, zlib两个库 下载地址: libpng:http://libmng.com/pub/png/libpn ...

  2. OC中@class的使用

    作用: 可以简单地引用一个类简单使用@class Dog; //类的引入 #import "Dog.h"仅仅是告诉编译器:Dog是一个类;并不会包含Dog这个类的所有内容 具体使用 ...

  3. 学习MVC之租房网站(十)-预约和跟单

    在上一篇<学习MVC之租房网站(九)-房源显示和搜索>完成了房源的显示.检索,然后是用户的预约看房,以及后台操作员对预约看房的跟单操作. 预约看房仅有将预约信息保存到对应表的操作,预约表有 ...

  4. JQuery控制下拉列表

    //遍历option和添加.移除option function changeShipMethod(shipping){ var len = $("select[@name=ISHIPTYPE ...

  5. 【企业级框架整合】Springmvc+mybatis+restful+bootstrap框架整合

    1. 使用阿里巴巴Druid连接池(高效.功能强大.可扩展性好的数据库连接池.监控数据库访问性能.支持Common-Logging.Log4j和JdkLog,监控数据库访问)2. 提供高并发JMS消息 ...

  6. 【JAVAWEB学习笔记】24_filter实现自动登录和解决全局的编码问题

    过滤器Filter 学习目标 案例-自动登录 案例-解决全局的编码 一.过滤器Filter 1.filter的简介 filter是对客户端访问资源的过滤,符合条件放行,不符合条件不放行,并且可以对目标 ...

  7. 一、 开篇(ASP.NET MVC5 系列)

    这个教程将教你一些用VS2013创建ASP.NET MVC 5 Web应用程序基础知识.为了避免开发工具的不一致而带来的一些小麻烦,建议你使用和我一样的开发工具VS2013英文版. 开发工具:Visu ...

  8. Spring+SpringMVC+MyBatis深入学习及搭建(四)——MyBatis输入映射与输出映射

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6878529.html 前面有讲到Spring+SpringMVC+MyBatis深入学习及搭建(三)——My ...

  9. angular实现的文字上下无缝滚动

    最近在学习angularJs,业余时间随便写了一个文字上下无缝滚动的例子,主要写了一个小小的指令. css代码:主要控制样式 <style type="text/css"&g ...

  10. sql备份文件兼容性问题

    第一步: 右键需要备份的数据库,选择"属性"