Learning to use the system. Collections. Specialized. namevaluecollection class of ASP. NET

Source: Internet
Author: User
Tags allkeys
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 ;}
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.