Use sortedlist in C #

Source: Internet
Author: User
  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. LINQ;
  4. Using system. text;
  5. Namespace set
  6. {
  7. Class ordered table
  8. {
  9. Public static void main ()
  10. {
  11. // If you want to use a sorted table, you can use sortedlist <tkey, tvalue> to sort the elements.
  12. Sortedlist <string, string> books = new sortedlist <string, string> ();
  13. Books. Add ("Aladdin", "64kb@163.com ");
  14. Books. Add ("zhaohaifu", "65kb@163.com ");
  15. Books. Add ("Jacky", "66kb@163.com ");
  16. Books. Add ("fuck", "67kb@163.com ");
  17. // Duplicate keys are not allowed. Next we use the add method to add Aladdin again.
  18. // Books. Add ("Aladdin", "sdaf"); // an exception occurs.
  19. // However, if the index is used to assign values, if the key exists, the value is overwritten. If the key does not exist, the value is equivalent to add.
  20. Books ["Aladdin"] = "haha_new ";
  21. Foreach (string STR in books. Keys)
  22. {
  23. Console. writeline (STR );
  24. }
  25. Foreach (string STR in books. values)
  26. {
  27. Console. writeline (STR );
  28. }
  29. // One-time traversal of the value Key
  30. Foreach (keyvaluepair <string, string> book in books)
  31. {
  32. Console. writeline ("Name: {0} Email: {1}", Book. Key, Book. value );
  33. }
  34. // From the above results, we can see that Aladdin is replaced and its value is changed to haha_new.
  35. // The following describes the methods and attributes in sortedlist <tkey, tvalue>.
  36. // The capacity attribute is used to set the capacity of an ordered table. Like ilist, it also doubles.
  37. // Comparer returns the comparator related to the ordered table. You can input the comparator from the constructor.
  38. // Remove () removeat () Key deletion and index Deletion
  39. // Containskey (); containsvalue (); check whether a key or value contains the specified value
  40. // Trygetvalue (); try to get the value of the specified key. If yes, true is used and out is used to bring the value back. If no value is flase
  41. // Click Delete
  42. Console. writeline ("------------------------- press the key to delete --------------");
  43. Books. Remove ("Aladdin ");
  44. Foreach (keyvaluepair <string, string> book in books)
  45. {
  46. Console. writeline ("Name: {0} Email: {1}", Book. Key, Book. value );
  47. }
  48. // You can see that Aladdin has been deleted.
  49. // Delete by index
  50. Books. removeat (0 );
  51. Console. writeline ("------------------------- Delete by index --------------");
  52. Foreach (keyvaluepair <string, string> book in books)
  53. {
  54. Console. writeline ("Name: {0} Email: {1}", Book. Key, Book. value );
  55. }
  56. // The result shows that the fuck is deleted, which indicates that the index is deleted in the insert order, not in the sorted order.
  57. // Check whether Aladdin exists; check whether zhaohaifu (key) exists)
  58. Console. writeline ("whether to include Aladdin key: {0}", Books. containskey ("Aladdin"); // false
  59. Console. writeline ("whether to include zhaohaifu key: {0}", Books. containskey ("zhaohaifu"); // true
  60. // Check whether contains 64kb@163.com check whether contains 64kb@163.com (value)
  61. Console. writeline ("include Aladdin key: {0}", Books. containsvalue ("64kb@163.com"); // false
  62. Console. writeline ("include zhaohaifu key: {0}", Books. containsvalue ("65kb@163.com"); // true
  63. // The index values here are sorted.
  64. Int keyindex = books. indexofkey ("zhaohaifu ");
  65. Console. writeline ("zhaohaifu index value: {0}", keyindex );
  66. String value = "";
  67. If (books. trygetvalue ("Aladdin", out value ))
  68. {
  69. Console. writeline ("got Aladdin value: {0}", value );
  70. }
  71. String value2 = "";
  72. If (books. trygetvalue ("zhaohaifu", out value2 ))
  73. {
  74. Console. writeline ("zhoahaifu value obtained: {0}", value2 );
  75. }
  76. Console. Readline ();
  77. // Note: this class is a wildcard slice of sortedlist. The class corresponding to the object version exists with the same name.
  78. }
  79. }
  80. }

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.