集合-字典(Dictionary)

字典(散列表):允许按照某个键来访问元素,能根据键快速查找元素,也可以自由添加,删除元素。比较像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()[0];
            int numberlength = id.Length - 1;
            try
            {
                number = int.Parse(id.Substring(1, numberlength > 6 ? 6 : 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 << 16) * 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>(10);

            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);
                }
            }
        }
    }
原文地址:https://www.cnblogs.com/hometown/p/3222970.html