Usage of HashTable in C #

Source: Internet
Author: User

1. Brief description of the hash table (Hashtable)

In. NET Framework, Hashtable is a capacity provided by the System. Collections namespace.

Is used to process and display key-value pairs similar to keyvalue. The key is usually used for quick search.

Key is case sensitive; value is used to store the value corresponding to the key. All keyvalue pairs in Hashtable

It is of the object type, so Hashtable can support any type of keyvalue key-value pairs.

 


Ii. Simple operations on Hash Tables

 


Add a keyvalue key-value pair in the hash table: HashtableObject. Add (key, value );

 


Remove a keyvalue key-value pair in the hash table: HashtableObject. Remove (key );

 


Remove all elements from the hash table: HashtableObject. Clear ();

 


Determine whether the hash table Contains the specified key: HashtableObject. Contains (key );

 


The following console contains all the preceding operations:


[Csharp]
Using System;
Using System. Collections; // file this namespace must be introduced when Hashtable is used.
Class hashtable
{
Public static void Main ()
{
Hashtable ht = new Hashtable (); // file to create a Hashtable instance
Ht. Add (E, e); // Add a keyvalue pair
Ht. Add (A, );
Ht. Add (C, c );
Ht. Add (B, B );
String s = (string) ht [A];
If (ht. Contains (E) // file to determine whether a hash table Contains a specific key. The return value is true or false.
Console. WriteLine (the E keyexist );
Ht. Remove (C); // Remove a keyvalue pair
Console. WriteLine (ht [A]); // output
Ht. Clear (); // remove all elements
Console. WriteLine (ht [A]); // file there will be no output here
}
}

Using System;
Using System. Collections; // file this namespace must be introduced when Hashtable is used.
Class hashtable
{
Public static void Main ()
{
Hashtable ht = new Hashtable (); // file to create a Hashtable instance
Ht. Add (E, e); // Add a keyvalue pair
Ht. Add (A, );
Ht. Add (C, c );
Ht. Add (B, B );
String s = (string) ht [A];
If (ht. Contains (E) // file to determine whether a hash table Contains a specific key. The return value is true or false.
Console. WriteLine (the E keyexist );
Ht. Remove (C); // Remove a keyvalue pair
Console. WriteLine (ht [A]); // output
Ht. Clear (); // remove all elements
Console. WriteLine (ht [A]); // file there will be no output here
}
}

 


3. traverse the hash table

 


DictionaryEntry Object is required to traverse a hash table. The Code is as follows:

[Csharp]
For (DictionaryEntry de in ht) fileht is a Hashtable instance.
{
Console. WriteLine (de. Key); // de. Key corresponds to the key
Console. WriteLine (de. Value); // de. Key corresponds to the Key-value Pair
}

For (DictionaryEntry de in ht) fileht is a Hashtable instance.
{
Console. WriteLine (de. Key); // de. Key corresponds to the key
Console. WriteLine (de. Value); // de. Key corresponds to the Key-value Pair
}


4. Sort hash tables

 


Sort the hash table. The definition here is to re-arrange the keys in the key-Value Pair according to certain rules,

Actually, this definition cannot be implemented, because we cannot directly redo the key in Hashtable.

Arrangement. If Hashtable is required to provide the output of certain rules, a work und can be adopted:

[Csharp]
ArrayList akeys = new ArrayList (ht. Keys); // file do not forget to import System. Collections
Akeys. Sort (); // Sort files alphabetically.
For (string skey in akeys)
{
Console. Write (skey + );
Console. WriteLine (ht [skey]); output after sorting
}

ArrayList akeys = new ArrayList (ht. Keys); // file do not forget to import System. Collections
Akeys. Sort (); // Sort files alphabetically.
For (string skey in akeys)
{
Console. Write (skey + );
Console. WriteLine (ht [skey]); output after sorting
}

 

 

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.