[讀書筆記]資料字典

來源:互聯網
上載者:User

這是一個關於資料字典使用的例子

 

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。散列碼在整數取值地區上的分布相當均勻。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.