[读书笔记]数据字典

这是一个关于数据字典使用的例子

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;



namespace DictionaryExample

{

    public struct EmployeeId : IEquatable<EmployeeId>

    {

        private readonly char prefix;

        private readonly int number;



        public EmployeeId(string id)

        {

            if (id == null)

                throw new ArgumentNullException("id");

            

            prefix = (id.ToUpper())[0];

            int numLength = id.Length - 1;

            number = int.Parse(id.Substring(1, numLength > 6 ? 6 : numLength));

        }



        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 class Employee

    {

        private string name;

        private decimal salary;

        private readonly EmployeeId id;



        public Employee(EmployeeId id, string name, decimal salary)

        {

            this.id = id;

            this.name = name;

            this.salary = salary;

        }



        public override string ToString()

        {

            return String.Format("{0}: {1, -20} {2:C}", id.ToString(), name, salary);

        }



    }



    class Program

    {

        static void Main(string[] args)

        {

            Dictionary<EmployeeId, Employee> employees = new Dictionary<EmployeeId, Employee>(31);



            EmployeeId idJeff = new EmployeeId("C7102");

            Employee jeff = new Employee(idJeff, "Jeff Gordon", 5164580.00m);

            employees.Add(idJeff, jeff);

            Console.WriteLine(jeff);



            EmployeeId idTony = new EmployeeId("C7105");

            Employee Tony = new Employee(idTony, "Tony Stewart", 4814200.00m);

            employees.Add(idTony, Tony);

            Console.WriteLine(Tony);



            EmployeeId idDenny = new EmployeeId("C8011");

            Employee Denny = new Employee(idDenny, "Denny Hamlin", 3718710.00m);

            employees.Add(idDenny, Denny);

            Console.WriteLine(Denny);



            EmployeeId idCarl = new EmployeeId("F7908");

            Employee Carl = new Employee(idCarl, "Carl Edwards", 3285710.00m);

            employees[idCarl] = Carl;

            Console.WriteLine(Carl);



            EmployeeId idMatt = new EmployeeId("F7203");

            Employee Matt = new Employee(idMatt, "Matt Kensenth", 4520330.00m);

            employees.Add(idMatt, Matt);

            Console.WriteLine(Matt);



            while (true)

            {

                Console.Write("Enter employee id (X to exit)>");



                string userInput = Console.ReadLine();

                userInput = userInput.ToUpper();



                if (userInput == "X") break;



                EmployeeId id = new EmployeeId(userInput);

                Employee employee;

                if (!employees.TryGetValue(id, out employee))

                {

                    Console.WriteLine("Employee with id " + "{0} does not exist", id);

                }

                else

                {

                    Console.WriteLine(employee);

                }

            }



        }

    }

}



主要关注这里面的对于GetHashCode的重载的算法。这里GetHashCode使用的算法将数字向左移动16位,再与原来的数字进行异或操作,最后将结果乘以十六进制数15051505。散列码在整数取值区域上的分布相当均匀。

你可能感兴趣的:(读书笔记)