. Net (4.0) provides us with the following types of dictionary structures: Dictionary, SortedDictionary, Lookup. These types support the paradigm, and they work in the same principle, providing the ability to receive a health return value. The differences between them are:
Sorteddictionar: An ordered dictionary in which elements inserted into the dictionary are arranged in ascending order.
Lookup: A key can correspond to multiple values.
In the data structure of a dictionary, the value of the current object is not the true function of the key. Instead, the hash value of the object, which is passed through object. The GetHashCode () method returns the. According to the different objects should have different hash value principle, we can simply overload the object. GetHashCode method to set our own special key, let's take a look at a small example. (based on the results of the test code, we can also infer that the. NET CLR determines whether they are equal by matching the hash values of different objects.) )
class Person:iequatable<person>,icomparer<person> {public string firstName, LastName;
public int age; Public person () {} public person (string firstName, string lastName, int age) {THIS.FIRSTN
ame = FirstName;
This.lastname = LastName;
This.age = age; public boolean Equals (person other) {return to other. GetHashCode () = = this.
GetHashCode ();
Override int GetHashCode () {if (age!= null) {
Return age << 5 * 30; else {return base.
GetHashCode ();
} public int Compare (person x, person y) {if (x.age>y.age) {
return 1; else if (x.age = = y.age) {RetuRN 0;
else {return-1; }
}
}
public static void Testdictionarykey () {Who Jeff = new Person ("Jeff", "Xiong", 26);
Person Jeff2 = Jeff;
Console.WriteLine ("Jeff ' s Hash code:{0}", Jeff.gethashcode ());
Console.WriteLine ("Jeff2 ' hash Code:{0}", Jeff2.gethashcode ());
if (Jeff.equals (JEFF2)) {Console.WriteLine ("Jeff equal Jeff2");
else {Console.WriteLine ("Jeff Not Equal Jeff2");
Person Bob = new Person ("Bob", "Li", 26);
Console.WriteLine ("Bob ' s Hash code:{0}", Bob.gethashcode ());
if (Jeff.equals (Bob)) {Console.WriteLine ("Jeff equal Bob");
else {Console.WriteLine ("Jeff Not Equal Bob");
} string A = "Jeff"; String B = "JEFF ";
Console.WriteLine ("A ' s Hash code:{0}", A.gethashcode ());
Console.WriteLine ("B ' hash Code:{0}", B.gethashcode ());
Double AA = 1.23;
Double BB = 1.23; Console.WriteLine ("AA ' s Hash code:{0}", aa.
GetHashCode ()); Console.WriteLine ("BB ' hash code:{0}", Bb.
GetHashCode ()); /*out put Jeff ' s hash code:109051904 Jeff2 ' s hash code:109051904 Jeff E Qual Jeff2 Bob ' s hash code:109051904 Jeff equal Bob A ' s hash code:80892 4690 B ' s hash code:808924690 AA ' s hash code:1158867386 BB ' s hash code:1 158867386 */}