Hashtable and hashtable

Source: Internet
Author: User

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 ();}}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.