字典(散列表):允许按照某个键来访问元素,能根据键快速查找元素,也可以自由添加,删除元素。比较像List<T>类,但没有list向后移动元素的性能开销。

.net中最主要的字典类是Dictionary<Tkey,Tvalue>。

字典中的键:用作字典中键的类型必须重写Object类中的GetHashCode()方法。调用GetHashCode()方法主要是为了获得元素的位置。

键还必须实现IEquatable<T>.Equals方法或重写Object类的Equals()方法。因为不同的键代码可能返回相同的散列代码,所以使用equals方法比较键。字典比较两个键是否相等,调用A.Equals(B)方法,如果A.Equals(B)返回true,则A. GetHashCode()和B. GetHashCode()必须返回相同的散列代码!

 public class EmployeeIdException : Exception
{
public EmployeeIdException(string message) : base(message) { }
} public class EmployeeId : IEquatable<EmployeeId>
{
private readonly char prefix;
private readonly int number; public EmployeeId(string id)
{
if (id == null || id == "")
{
throw new ArgumentNullException("id");
}
prefix = id.ToUpper()[];
int numberlength = id.Length - ;
try
{
number = int.Parse(id.Substring(, numberlength > ? : numberlength)); }
catch (FormatException)
{
throw new EmployeeIdException("无法格式化ID");
}
} public override string ToString()
{
return prefix.ToString() + string.Format("{0,6:000000}", number);
} public override int GetHashCode()
{
return (number ^ number << ) * 0x15051505;
} public bool Equals(EmployeeId other)
{
return (prefix == other.prefix && number == other.number);
} public override bool Equals(object obj)
{
return Equals((EmployeeId)obj);
} public static bool operator ==(EmployeeId left, EmployeeId right)
{
return left.Equals(right);
} public static bool operator !=(EmployeeId left, EmployeeId right)
{
return !(left == right);
}
} public class Employee
{
private string name;
private double salary;
private EmployeeId id;
public Employee(string name, double salary, EmployeeId id)
{
this.name = name;
this.salary = salary;
this.id = id;
} public override string ToString()
{
return string.Format("{0}:{1,-20}{2:C}", id, name, salary);
}
}
class Program
{
static void Main(string[] args)
{
var employees = new Dictionary<EmployeeId, Employee>(); var idrxm = new EmployeeId("A5");
var rxm = new Employee("rxm", 9.6, idrxm);
employees.Add(idrxm, rxm);
Console.WriteLine(rxm); var idcwr = new EmployeeId("B888888456");
var cwr = new Employee("cwr", 9.5, idcwr);
employees.Add(idcwr, cwr);
Console.WriteLine(cwr); while (true)
{
Console.WriteLine("请输入员工工号,X代表退出!");
var writeTxt = Console.ReadLine();
if (writeTxt.ToLower() == "x")
{
break;
}
EmployeeId id;
try
{
id = new EmployeeId(writeTxt);
Employee employ;
if (!employees.TryGetValue(id, out employ))
{
Console.WriteLine("{0},这个工号的人不存在", id);
}
else
{
Console.WriteLine(employ);
}
}
catch (EmployeeIdException ex)
{
Console.WriteLine(ex.Message);
}
}
}
}

集合-字典(Dictionary)的更多相关文章

  1. C#中数组、集合(ArrayList)、泛型集合List<T>、字典(dictionary<TKey,TValue>)全面对比

    C#中数组.集合(ArrayList).泛型集合List<T>.字典(dictionary<TKey,TValue>)全面对比 为什么把这4个东西放在一起来说,因为c#中的这4 ...

  2. python 数据类型: 字符串String / 列表List / 元组Tuple / 集合Set / 字典Dictionary

    #python中标准数据类型 字符串String 列表List 元组Tuple 集合Set 字典Dictionary 铭记:变量无类型,对象有类型 #单个变量赋值 countn00 = '; #整数 ...

  3. 索引器、哈希表Hashtabl、字典Dictionary(转)

    一.索引器 索引器类似于属性,不同之处在于它们的get访问器采用参数.要声明类或结构上的索引器,使用this关键字. 示例:   索引器示例代码 /// <summary> /// 存储星 ...

  4. C# 字典 Dictionary<Tkey,Tvalue>

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来.我们都知道计算机技术发展日新月异,速度惊人的快,你我稍不留神,就会被慢慢淘汰!因此:每日不间断的学习是避免被 ...

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

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

  6. 你真的了解字典(Dictionary)吗? C# Memory Cache 踩坑记录 .net 泛型 结构化CSS设计思维 WinForm POST上传与后台接收 高效实用的.NET开源项目 .net 笔试面试总结(3) .net 笔试面试总结(2) 依赖注入 C# RSA 加密 C#与Java AES 加密解密

    你真的了解字典(Dictionary)吗?   从一道亲身经历的面试题说起 半年前,我参加我现在所在公司的面试,面试官给了一道题,说有一个Y形的链表,知道起始节点,找出交叉节点.为了便于描述,我把上面 ...

  7. [Python]字典Dictionary、列表List、元组Tuple差异化理解

    概述:Python中这三种形式的定义相近,易于混淆,应注意区分. aDict={'a':1, 'b':2, 'c':3, 'd':4, 'e':5} aList=[1,2,3,4,5] aTuple= ...

  8. C#字典 Dictionary<Tkey,Tvalue> 之线程安全问题 ConcurrentDictionary<Tkey,Tvalue> 多线程字典

    ConcurrentDictionary<Tkey,Tvalue>  Model #region 程序集 mscorlib, Version=4.0.0.0, Culture=neutra ...

  9. C#对字典Dictionary 的添加,遍历,移除系列操作

    C#对字典Dictionary 的添加,遍历,移除系列操作: //一.创建泛型哈希表,然后加入元素 Dictionary<string, string> oscar = new Dicti ...

  10. C#创建安全的字典(Dictionary)存储结构

    在上面介绍过栈(Stack)的存储结构,接下来介绍另一种存储结构字典(Dictionary). 字典(Dictionary)里面的每一个元素都是一个键值对(由二个元素组成:键和值) 键必须是唯一的,而 ...

随机推荐

  1. JS电子文档链接

    http://www.oschina.net/translate/learning-javascript-design-patterns   学用 JavaScript 设计模式 http://es6 ...

  2. LeetCode OJ 202. Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  3. OpenCV ——双线性插值(Bilinear interpolation)

    1,原理 在图像的仿射变换中,很多地方需要用到插值运算,常见的插值运算包括最邻近插值,双线性插值,双三次插值,兰索思插值等方法,OpenCV提供了很多方法,其中,双线性插值由于折中的插值效果和运算速度 ...

  4. 在centos中部署jenkins

    在centos中部署jenkins,需要的环境:安装jdk,Apache-tomcat 这两步我前面文章里已写,再次忽略 到官网下载最新的jenkins 我这里的是  jenkins.war 把该文件 ...

  5. 使用SQL Server 2000索引视图提高性能

    什么是索引视图? 许多年来,Microsoft? SQL Server? 一直都提供创建虚拟表(称为视图)的功能.在过去,这些视图主要有两种用途: 提供安全机制,将用户限制在一个或多个基表中的数据的某 ...

  6. 状压dp找寻环的个数 Codeforces Beta Round #11 D

    http://codeforces.com/problemset/problem/11/D 题目大意:给你n个点,m条边,找该图中有几个换 思路:定义dp[i][j]表示i是圈的集合,j表示该集合的终 ...

  7. c++判断一个字符串是否是数字

    bool isNum(const string& str) { bool bRet = false; bool point = false; ) { return bRet; } ]) &am ...

  8. 【滚动数组】 dp poj 1036

    题意:一群匪徒要进入一个酒店.酒店的门有k+1个状态,每个匪徒的参数是:进入时间,符合的状态,携带的钱. 酒店的门刚开始状态0,问最多这个酒店能得到的钱数. 思路: dp数组为DP[T][K]. 转移 ...

  9. 封装sdk API 应用

    1 #include "QWinApp.h" 2 #include "QGlobal.h" 3 int WINAPI _tWinMain(HINSTANCE h ...

  10. Openlayer 3 图层列表控件(自定义)

    <body><div id="map"></div><div id="layerControl" class=&quo ...