Dictionaryentry is a simple structure that contains a pair of key/value values;
Hashtable (hash table) is a set of keys and values. It is precisely a set of dictionaryentries.
Dictionaryentry example:
Protected void button#click (Object sender, eventargs e) {dictionaryentry D1; d1.key = "K1"; d1.value = 123; dictionaryentry D2 = new dictionaryentry ("K2", 456 ); textbox1.text = string. concat (d1.key, ":", d1.value, ";", d2.key, ":", d2.value); // K1: 123; K2: 456}
Hashtable member:
/* Static Method */hashtable. synchronized (); /// * attribute */count // number of elements isfixedsize // isreadonly // issynchronized // keys // key set syncroot // values // value set/* Method */Add () // Add clear () // clear clone () // contains () // whether to contain the specified key, the same as containskey () // whether to contain the specified keycontainsvalue () // include the specified valuecopyto () // copy to equals () // getenumerator () // getobjectdata () // ondeserialization () // remove () // remove/* Extension Method */asparallel () // asqueryable () // cast () // oftype ()//
Start with the exercise:
Protected void button#click (Object sender, eventargs e) {hashtable hash = new hashtable (); hash. add ("K1", "AAAAA"); hash. add ("K2", "bbbbb"); hash. add ("K3", "CCCCC"); hash. add ("K4", "ddddd"); int n1 = hash. count; // 4 string S1 = hash ["K2"]. tostring (); // bbbbb hash ["K2"] = "12345"; string S2 = hash ["K2"]. tostring (); /// 12345 hash. remove ("K2"); int n2 = hash. count; // 3 hash. clear (); int N3 = hash. count; // 0 textbox1.text = string. concat (N1, "\ n", S1, "\ n", S2, "\ n", N2, "\ n", N3 );}
Traversal:
Protected void button#click (Object sender, eventargs e) {hashtable hash = new hashtable (); hash. add (1, "AAAAA"); hash. add (2, "bbbbb"); hash. add (3, "CCCCC"); hash. add (4, "ddddd"); string STR = ""; foreach (dictionaryentry de in hash) {STR + = string. format ("{0 }:{ 1} \ n", de. key, de. value);} textbox1.text = STR;}/* traversal result: 4: ddddd3: ccccc2: bbbbb1: AAAAA **********/
Contains (), containskey (), containsvalue ():
Protected void button#click (Object sender, eventargs e) {hashtable hash = new hashtable (); hash. add ("K1", 123); hash. add ("K2", "ABC"); hash. add ("K3", 3.14); hash. add ("K4", null); bool b1 = hash. contains ("K1"); // true bool b2 = hash. contains ("K4"); // true bool B3 = hash. contains ("K5"); // false bool B4 = hash. containskey ("K1"); // true bool B5 = hash. containskey ("K4"); // true bool B6 = hash. containsk Ey ("K5"); // false bool B7 = hash. containsvalue ("3.14"); // false bool B8 = hash. containsvalue (3.14); // true bool B9 = hash. containsvalue (null); // true bool b0 = hash. containsvalue (""); // false textbox1.text = string. format ("{0} \ n {1} \ n {2} \ n {3} \ n {4} \ n {5} \ n {6} \ n {7} \ n {8} \ n {9 }", b1, B2, B3, B4, B5, B6, B7, B8, B9, B0);} protected void button2_click (Object sender, eventargs e) {hashtable hash = new hasht Able (); If (! Hash. contains ("K1") {hash. add ("K1", datetime. now);} If (hash. contains ("K1") {hash. remove ("K1 ");}}
Keys, values:
Protected void button#click (Object sender, eventargs e) {hashtable hash = new hashtable (); hash. add ("K1", "AAA"); hash. add ("K2", "BBB"); hash. add ("K3", "CCC"); hash. add ("K4", "DDD"); icollection Ks = hash. keys; icollection Vs = hash. values; string S1 = "", S2 = ""; foreach (string K in KS) {S1 + = K + ";" ;}// K4; K1; K2; k3; foreach (string V in VS) {S2 + = V + ";" ;}// DDD; AAA; BBB; CCC; textbox1.text = S1 + "\ n" + S2 ;}