Posted by
admin 18 February, 2011
->
This is an example of use on the data dictionary
1
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DictionaryExample { public struct EmployeeId : IEquatable { 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 employees = new Dictionary(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); } } } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DictionaryExample {public struct EmployeeId: IEquatable {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 employees = new Dictionary (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);}}}}}
Inside the main concern for this overloaded method GetHashCode. GetHashCode algorithm used here, move left 16-bit number, then the figures with the original XOR, and finally multiplying the result by the hexadecimal number 15051505. An integer hash code value in the region fairly evenly distributed on.
Categories :
book review Tags :
Comments
No comments yet.