Namevaluecollection is equivalent to a hash table whose keys and values are strings and can be accessed through indexes.
Main members:
/* Attribute */allkeys; // return string [] count; // keys; // key set/* Method */Add (); // clear (); // copyto (); // get (); // getkey () is separated by commas when multiple values exist based on the index or key value (); // getvalues () based on the index; // returns string [] haskeys () based on the index or key value; // determines whether a non-null key is included in remove (); // remove set () according to the key; // modify the value; if the key does not exist, it is the same as add ()
The key can be null and can correspond to multiple values:
Protected void button1_click (Object sender, eventargs e) {namevaluecollection NV = new namevaluecollection (); NV. add ("K1", "AAA"); NV. add ("K2", "BBB"); NV. add ("K3", "CCC"); NV. add (null, "DDD"); NV. add ("K2", "B"); NV. add ("K2", "BB"); NV. add ("K2", "BBB"); string str1 = NV ["K1"]; // AAA string str2 = NV ["K2"]; // BBB, B, BB, BBB: multiple values are separated by commas (,). String str3 = NV [1]; // BBB, B, BB, BBB string str4 = NV [null]; // DDD textbox1.text = str1 + "\ n" + str2 + "\ n" + str3 + "\ n" + str4 ;}
Exercise:
Protected void button1_click (Object sender, eventargs e) {namevaluecollection NV = new namevaluecollection (); NV. add ("K1", "AAA"); NV. add ("K2", "BBB"); NV. add ("K3", "CCC"); NV. add ("K2", "B"); NV. add ("K2", "BB"); NV. add ("K2", "BBB"); int n = NV. count; // 3: K1, K2, K3 NV. set ("K1", "AAA"); string str1 = NV. get (0); // AAA string str2 = NV. get ("K1"); // AAA string str3 = NV. getkey (0); // K1 string [] sarr1 = NV. getvalues (0); string [] sarr2 = NV. getvalues ("K2"); string str4 = string. join (";", sarr1); // AAA string str5 = string. join (";", sarr2); textbox1.text = string. format ("{0} \ n {1} \ n {2} \ n {3} \ n {4} \ n {5}", N, str1, str2, str3, str4, str5);} protected void button2_click (Object sender, eventargs e) {namevaluecollection NV = new namevaluecollection (); NV. add ("K1", "AAA"); NV. add ("K2", "BBB"); NV. add ("K3", "CCC"); NV. add ("K2", "B"); NV. add ("K2", "BB"); NV. add ("K2", "BBB"); namevaluecollection. keyscollection keys = NV. keys; string str1 = ""; foreach (string s in keys) {str1 + = S + "," ;}// K1, K2, K3, string str2 = string. join (";", NV. allkeys); // K1; K2; K3 textbox1.text = str1 + "\ n" + str2 ;}