Hashtable and hashtable
Hashtable
In System. Collection, The namespace Li Hashtable is a class that programmers often use. It is known for its rapid retrieval and is an indispensable tool for R & D personnel development.
Hashtable indicates the set of key/value pairs. These key/value pairs are organized according to the hash code of the key. The key of Hashtable must be unique and there is no valid sorting. It performs internal sorting.
Hashtable has the following 4 traversal methods:
1. Use the string object as the key value to traverse the hash table.
2. Use a custom object as the key value to traverse the hash table.
3. Use the DictionaryEntry object as the key value to traverse the hash table.
4. traverse the hash table by inheriting the objects of the IDictionaryEnumerator interface.
Using System; using System. collections; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace ConsoleApp1 {class Program {class Person {private int age; public int Age {get {return age;} set {age = value ;}} private string name; public string Name {get {return name;} set {name = value;} private string email; public string Email {get {return email ;} set {email = value ;}} static void Main (string [] args) {var a = new Person {Age = 34, Name = "Jacky ", email = "Jacky@gmail.com"}; var B = new Person {Age = 23, Name = "Ajay", Email = "Ajay@gmail.com"}; var c = new Person {Age = 12, name = "Bill", Email = "Bill@gmail.com"}; var d = new Person {Age = 23, Name = "Gace", Email = "Gace@gmail.com "}; var e = new Person {Age = 45, Name = "Jim", Email = "Jim@gmail.com"}; var ht = new Hashtable {"1", }, {"2", B}, {"3", c}, {"4", d}, {"5", e}; Console. writeLine ("Enter your query Username:"); var strName = Console. readLine (); // The first method foreach (string item in ht. keys) {var p = (Person) ht [item]; if (strName = p. name) {Console. writeLine ("the query result is:" + p. name + "\ t" + p. email + "\ t" + p. age) ;}} Console. writeLine ("gorgeous split line ========================================== =================================== "); // method 2 foreach (Person item in ht. values) {if (item. name = strName) {Console. writeLine ("the query result is:" + item. name + "\ t" + item. email + "\ t" + item. age) ;}} Console. writeLine ("gorgeous split line ========================================== =================================== "); // method 3: foreach (DictionaryEntry item in ht) {if (strName = (Person) item. value ). name) {Console. writeLine ("the query result is:" + (Person) item. value ). name + "\ t" + (Person) item. value ). email + "\ t" + (Person) item. value ). age) ;}} Console. writeLine ("gorgeous split line ========================================== =================================== "); // Method 4: IDictionaryEnumerator id = ht. getEnumerator (); while (id. moveNext () {Person p = (Person) ht [id. key]; if (p. name = strName) {Console. writeLine ("the query result is:" + p. name + "\ t" + p. email + "\ t" + p. age) ;}} Console. readKey ();}}}