Learning to use the system. Collections. sortedlist class of ASP. NET

Source: Internet
Author: User
Sortedlist is a "key/value" list class that can be automatically sorted (sort by key) and can access elements through indexes.

It is like upgrading hashtable (hash table). Every element of them is regarded as a dictionaryentry (key/value) structure.

Because it has more sorting and indexing than hashtable, it is less efficient than hashtable.

Main members:
 
/* Attribute */capacity; // capacity count; // number of elements keys; // icollection values; // icollection) /* Method */Add () // Add clear () // clear contains () // whether the specified key containskey () // same as contains () containsvalue () // include the specified value getbyindex () // obtain valuegetkey () based on the index // obtain the keygetkeylist () based on the index // obtain the Key List (ilist) getvaluelist () // Value List (ilist) indexofkey () // obtain the index indexofvalue () of the specified key // obtain the index of the specified value remove () // Delete removeat () according to the key value () // Delete setbyindex () based on the index // trimtosize () based on the index setting value // optimize the capacity (capacity = count)
The elements are automatically sorted:
 
Protected void button1_click (Object sender, eventargs e) {sortedlist slist = new sortedlist (); slist. add ("2 k", "x"); // The value can be repeated, and the key cannot be repeated slist. add ("1 K", "XX"); slist. add ("4 K", "XXX"); slist. add ("3 K", "XXXX"); string STR = ""; foreach (dictionaryentry de in slist) {STR + = string. format ("{0 }:{ 1} \ n", de. key, de. value);} textbox1.text = STR;}/******** 1 K: xx2k: x3k: xxxx4k: XXX *********/
Valid value:
Protected void button1_click (Object sender, eventargs e) {sortedlist slist = new sortedlist (); slist. add ("K1", "AAA"); slist. add ("K2", "BBB"); slist. add ("K3", "CCC"); slist. add ("K4", "DDD"); string S1 = slist ["K2"]. tostring (); // BBB string S2 = slist. getbyindex (1 ). tostring (); // BBB string S3 = slist. getkey (1 ). tostring (); // K2 textbox1.text = S1 + "\ n" + S2 + "\ n" + S3 ;}
Change Value:
 
Protected void button1_click (Object sender, eventargs e) {sortedlist slist = new sortedlist (); slist. add ("4", "AAA"); slist. add ("3", "BBB"); slist. add ("2", "CCC"); slist. add ("1", "DDD"); slist ["4"] = "AAA"; slist. setbyindex (1, "CCC"); string STR = ""; for (INT I = 0; I
Delete elements and retrieve indexes:
 
Protected void button1_click (Object sender, eventargs e) {sortedlist slist = new sortedlist (); slist. add ("4", "AAA"); slist. add ("3", "BBB"); slist. add ("2", "CCC"); slist. add ("1", "DDD"); If (slist. contains ("2") {slist. remove ("2");} // Delete 2/CCC if (slist. count> 0) {slist. removeat (slist. count-1);} // the index of the remaining elements will be deleted. 4/AAA // search for the index int n1 = slist. indexofkey ("3"); // 1 int n2 = slist. indexofkey ("1"); // 0 int N3 = slist. indexofvalue ("BBB"); // 1 int N4 = slist. indexofvalue ("DDD"); // 0 slist. clear (); int N5 = slist. indexofkey ("3"); //-1:-1 int N6 = slist. indexofvalue ("BBB"); //-1 textbox1.text = string. concat (N1 + "\ n" + N2 + "\ n" + N3 + "\ n" + N4 + "\ n" + N5 + "\ n" + N6 );}
Capacity, count, and trimtosize ():
Protected void button#click (Object sender, eventargs e) {sortedlist slist = new sortedlist (100); // reserved space for storing 100 elements int n1 = slist. capacity; // 100 int n2 = slist. count; // 0 slist. add (1,111); slist. add (2,222); int N3 = slist. capacity; // 100 int N4 = slist. count; // 2 slist. trimtosize (); int N5 = slist. capacity; // 2 int N6 = slist. count; // 2 textbox1.text = string. concat (N1 + "\ n" + N2 + "\ n" + N3 + "\ n" + N4 + "\ n" + N5 + "\ n" + N6 );}
Obtain the key set, value set, key list, and Value List respectively:
Protected void button1_click (Object sender, eventargs e) {sortedlist slist = new sortedlist (5); slist. add ("e", 5.55); slist. add ("C", 3.33); slist. add ("D", 4.44); slist. add ("A", 1.11); slist. add ("B", 2.22); icollection keys = slist. keys; icollection values = slist. values; ilist keylist = slist. getkeylist (); ilist ValueList = slist. getvaluelist (); string S1, S2, S3, S4; S1 = S2 = S3 = S4 = ""; foreach (var k in keys) {S1 + = K. tostring () + "";} // a B c d e foreach (VAR V in values) {S2 + = v. tostring () + "";} // 1.11 2.22 3.33 4.44 5.55 for (INT I = 0; I

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.