Use of hashtable in C- [. NET technology C # class]
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 );
WindowsProgramAll of the above operations will be included:
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,CodeAs follows:
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:
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
}
V. Example:
1 Public Void Initdata ()
2 {
3 String SQL = @" Select * from orders " ;
4
5 Sqlconnection con = New Sqlconnection ( " Server =.; uid = sa; Pwd = sa; database = northwind " );
6 Sqldataadapter SDA = New Sqldataadapter (SQL, con );
7 Datatable dt = New Datatable ();
8 SDA. Fill (DT );
9
10 Hashtable htT = New Hashtable ( 5 );
11
12 Foreach (Datarow Dr In DT. Rows)
13 {
14 Htt. Add (Dr [ " Orderid " ], Dr [ " Customerid " ]);
15
16 }
17 Arraylist al = New Arraylist (HTT. Keys );
18 Al. Sort ();
19 Foreach (Dictionaryentry de In HTT)
20 {
21 If (De. value. tostring = " Alfki " )
22 {
23 Response. Write (De. Key + " Next. " );
24 }
25 }
26 Gridview1.datasource = DT;
27 Gridview1.databind ();
28
29 }