Summary of hash table usage in c #

Source: Internet
Author: User
Tags key string
Hash Tables may be familiar to many peers. it was a little strange at first, and later I got familiar with it when I used it too much. of course there are many introductions on this knowledge point on the Internet, but this does not prevent me from making my own summary and understanding.

In. in the. NET Framework, Hashtable is System. A container provided by the Collections namespace is used to process and present key-value pairs similar to key/value. The key is usually used for quick search, and the key is case sensitive; value is used to store the value corresponding to the key. In Hashtable, key/value pairs are of the object type, so Hashtable can support any type of key/value pairs. therefore, in the process of use, you must reference: using System. collections; below is a list of common usage, and corresponding comments are provided on the right. i. common Methods: Hashtable hshTable = new Hashtable (); // create a hash table hshTable. add ("Person1", "zhanghf"); // Add the key-Value Pair hshTable to the hash table. clear (); // remove all key-value pairs in the hash table. contains ("Person1"); // determines whether the hash table Contains the key string name = (string) hshTable ["Person1"]. toString (); // obtain the hshTable value of the specified key in the hash table. remove ("Person1"); // Delete the key-Value Pair IDictionaryEnumerator en = hshTable in the hash table. getEnumerator (); // traverses all the keys in the hash table and reads the corresponding values.
While (en. MoveNext ())
{
String str = en. Value. ToString ();
}
Ii. traverse the hash table: DictionaryEntry Object is required to traverse the hash table. The Code is as follows:
For (DictionaryEntry de in ht) // ht is a Hashtable instance.
{
Console. WriteLine (de. Key); // de. Key corresponds to the key-value Pair
Console. WriteLine (de. Value); // de. Key corresponds to key/value Key-value Pair Value}
Iii. Sorting hash tables
The definition of sorting hash tables here is to re-arrange the keys in key/value pairs according to certain rules, but in fact this definition cannot be implemented, because we cannot re-arrange keys directly in Hashtable, if Hashtable needs to provide output of certain rules, we can adopt a work und:
ArrayList akeys = new ArrayList (ht. Keys); // remember to import System. Collections
Akeys. Sort (); // calls the Sort of akeys in alphabetical order. This is easy to implement independently.
For (string skey in akeys)
{
Console. Write (skey + ":");
Console. WriteLine (ht [skey]); // output after sorting
} Hash tables are widely used in C # programming and have powerful functions. It is a good thing to master and be familiar with the use of hash tables!

This article from the "men no longer silence" blog, please be sure to keep this source http://zhanghf.blog.51cto.com/193856/33168

This article is from 51CTO. COM technical blog

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.