Use of HashTable in C #

Source: Internet
Author: User

I. Brief introduction to the hash table (Hashtable)
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.
Ii. Simple operations on Hash Tables
Add a key/value pair in the hash table: HashtableObject. Add (key, value );
Remove a 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 Windows program will include all the above operations:
 
[Csharp]
Private void button#click (object sender, System. EventArgs e)
{
Hashtable ht = new Hashtable (); // create a Hashtable instance
 
Ht. Add ("E", "e"); // Add key/value pairs
Ht. Add ("A", "");
Ht. Add ("C", "c ");
Ht. Add ("B", "B ");
 
MessageBox. Show ("the value of key A is:" + (string) ht ["A"]);
MessageBox. show ("whether the key E:" + ht. contains ("E "). toString (); // determines whether the hash table contains a specific key. The return value is true or false.

Ht. Remove ("C"); // Remove a key/value pair
MessageBox. Show ("C key removed:" + ht ["C"]);
 
Ht. Clear (); // remove all elements
MessageBox. Show ("all elements have been removed" + ht ["A"]); // No output will be provided here
}

 
3. traverse the hash table
DictionaryEntry Object is required to traverse a hash table. The Code is as follows:
[Csharp]
Foreach (DictionaryEntry de in ht) // ht is a Hashtable instance.
{
MessageBox. Show (de. Key. ToString (); // de. Key corresponds to key/value pairs
MessageBox. Show (de. Value. ToString (); // de. Key corresponds to the key/value Key-value Pair Value
}

 
4. 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:
[Csharp]
ArrayList akeys = new ArrayList (ht. Keys); // do not forget to import System. Collections
Akeys. Sort (); // Sort by letter
Foreach (string skey in akeys)
{
MessageBox. Show (skey + ":");
MessageBox. Show (ht [skey]. ToString (); // output after sorting
}

 
[Csharp]
Public void InitData ()
{
String SQL = @ "select * from orders ";
 
SqlConnection con = new SqlConnection ("server =.; uid = sa; pwd = sa; database = NorthWind ");
SqlDataAdapter sda = new SqlDataAdapter (SQL, con );
DataTable dt = new DataTable ();
Sda. Fill (dt );
 
Hashtable htt = new Hashtable (5 );
 
Foreach (DataRow dr in dt. Rows)
{
Htt. Add (dr ["orderID"], dr ["mermerid"]);
 
}
ArrayList al = new ArrayList (htt. Keys );
Al. Sort ();
Foreach (DictionaryEntry de in htt)
{
If (de. Value. ToString = "ALFKI ")
{
Response. Write (de. Key + "Next .");
}
}
GridView1.DataSource = dt;
GridView1.DataBind ();
}

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.