字典(散列表):允许按照某个键来访问元素,能根据键快速查找元素,也可以自由添加,删除元素。比较像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. List------Linked 链表

    1.Definition Linked list consists of a series of nodes. Each nodes contains the element and a pointe ...

  2. WisKey的眼神

    WisKey的眼神 Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Sub ...

  3. Centos中压缩(zip)和解压(unzip)命令

    摘自:http://liuzhichao.com/p/681.html 1.我下载了一个yasuo.zip文件,想解压缩: # unzip yasuo.zip 2.我当前目录下有abc1.zip,ab ...

  4. Spring Security-用户密码自定义加密

    public class SunPasswordEncoder implements PasswordEncoder{ //@实现加密的方法,既将明文转换为密文的方法 public String en ...

  5. C陷阱与缺陷 第一章

    1. 使用 e1=e2的赋值方式 作为 条件语句内部的判断,请使用显示的判断 不使用: if( x =y ) foo(); 而使用: ) foo(); 2. 注意编码规范,一定要在赋值号 “=”两边, ...

  6. selenium2(WebDriver) API

    selenium2(WebDriver) API 作者:Glen.He出处:http://www.cnblogs.com/puresoul/  1.1  下载selenium2.0的包 官方downl ...

  7. redis 队列缓存 + mysql 批量入库 + php 离线整合

    问题分析 思考:应用网站架构的衍化过程中,应用最新的框架和工具技术固然是最优选择:但是,如果能在现有的框架的基础上提出简单可依赖的解决方案,未尝不是一种提升自我的尝试. 解决: 问题一:要求日志最好入 ...

  8. 拖拽加点ui吧

    有一段时间没写东西了,真的是静下心来不容易的事情. 把之前的代码再翻看下,想想要加点ui什么的. 2d  塔防ui之前 我看过,但放到3d上做,其实难度是很大的. 不过,这不算什么,关键是 合理的 布 ...

  9. 用python计算md5,sha1,crc32

    Linux下计算md5sum,sha1sum,crc: 命令 输出 $md5sum hello f19dd746bc6ab0f0155808c388be8ff0  hello $sha1sum hel ...

  10. java 文件字节输出流

    Example10_5.java import java.io.*; public class Example10_5 { public static void main(String args[]) ...