字典(散列表):允许按照某个键来访问元素,能根据键快速查找元素,也可以自由添加,删除元素。比较像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. 第十九节,基本数据类型,集合set

    集合set,无序,是不允许重复内容的,也就是不允许重复元素,如果有重复,会自动忽略,可接收可迭代类型 (一般用于需要判断和处理交集时候用到) 集合与字典的区别是,集合没有键只有值,字典是有键的字典是一 ...

  2. php编译错误Note that the MySQL client library is not bundled anymore!

    Note that the MySQL client library is not bundled anymore! 解决方法. 1. 查看系统有没有安装mysql header find / -na ...

  3. java中的Unicode中文转义

    String ori = "\u5e7f\u4e1c"; public static String convertUnicode(String ori) { char aChar; ...

  4. js css优化-- 合并和压缩

    在项目框架中,首先要引用很多css和js文件,80%的用户响应时间都是浪费在前端.而这些时间主要又是因为下载图片.样式表.JavaScript脚本.flash等文件造成的.减少这些资源文件的Reque ...

  5. 用Chrome开发者工具做JavaScript性能分析

    来源: http://blog.jobbole.com/31178/ 你的网站正常运转.现在我们来让它运转的更快.网站的性能由页面载入速度和代码执行效率决定.一些服务可以让你的网站载入更快,比如压缩J ...

  6. Java中的一些术语的解释

    一  API(Application Programming Interface,应用程序编程接口) 简单来说,就是其他人开发出来一块程序,你想用,他会告诉你调用哪个函数,给这个函数传什么参数,然后又 ...

  7. javaScript 新学习:Array.contains 函数

    Array.contains 函数 确定指定对象是否是 Array 对象中的元素. 此函数是静态的,可在不创建对象实例的情况下调用. var itemExists = Array.contains(a ...

  8. OpenGL ES着色器语言之变量和数据类型(二)(官方文档第四章)

    OpenGL ES着色器语言之变量和数据类型(二)(官方文档第四章) 4.5精度和精度修饰符 4.5.1范围和精度 用于存储和展示浮点数.整数变量的范围和精度依赖于数值的源(varying,unifo ...

  9. Servlet Filter 过滤器

    Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源: 例如Jsp, Servlet, 静态图片文件或静态 ht ...

  10. android 代码动态创建视图

    LinearLayout 如何动态设置 margin? LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayou ...