C # Dictionary ultimate method of use http://www.yongfa365.com/Item/Dictionary.html)

Source: Internet
Author: User
  1. Using System;
  2. Using System. Collections. Generic;
  3. Class DictionaryDemo
  4. {
  5. Static void Main (string [] args)
  6. {
  7. DictionaryDemo001 ();
  8. Console. ReadLine ();
  9. DictionaryDemo002 ();
  10. Console. ReadLine ();
  11. DictionaryDemo003 ();
  12. Console. ReadLine ();
  13. }
  14. /// <Summary>
  15. /// General Usage
  16. /// </Summary>
  17. Static void DictionaryDemo001 ()
  18. {
  19. Dictionary <int, string> dict = new Dictionary <int, string> ();
  20. Dict. Add (1, "111 ");
  21. Dict. Add (2, "222 ");
  22. // Determine whether the corresponding key exists and display it
  23. If (dict. ContainsKey (2 ))
  24. {
  25. Console. WriteLine (dict [2]);
  26. }
  27. // Traverse Keys
  28. Foreach (var item in dict. Keys)
  29. {
  30. Console. WriteLine ("Key: {0}", item );
  31. }
  32. // Traverse Values
  33. Foreach (var item in dict. Values)
  34. {
  35. Console. WriteLine ("value: {0}", item );
  36. }
  37. // Traverse the entire dictionary
  38. Foreach (var item in dict)
  39. {
  40. Console. WriteLine ("key: {0} value: {1}", item. Key, item. Value );
  41. }
  42. }
  43. /// <Summary>
  44. /// The parameter is of another type
  45. /// </Summary>
  46. Static void DictionaryDemo002 ()
  47. {
  48. Dictionary <string, string []> dict = new Dictionary <string, string []> ();
  49. Dict. Add ("1", "1, 11, 111". Split (','));
  50. Dict. Add ("2", "2,22, 222". Split (','));
  51. Console. WriteLine (dict ["2"] [2]);
  52. }
  53. /// <Summary>
  54. /// Call a custom class
  55. /// </Summary>
  56. Static void DictionaryDemo003 ()
  57. {
  58. Dictionary <int, yongfa365> dict = new Dictionary <int, yongfa365> ();
  59. For (int I = 0; I <10; I ++)
  60. {
  61. Yongfa365 y = new yongfa365 ();
  62. Y. UserCode = I;
  63. Y. UserName = "www.yongfa365.com" + I. ToString ();
  64. Dict. Add (I, y );
  65. }
  66. Foreach (var item in dict)
  67. {
  68. Console. WriteLine ("{0} One: {1} UserName: {2}", item. Key, item. Value. UserCode, item. Value. UserName );
  69. }
  70. }
  71. }
  72. Class yongfa365
  73. {
  74. Public int UserCode {get; set ;}
  75. Public string UserName {get; set ;}
  76. }

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.