C # About dictionary

Source: Internet
Author: User

Introduction
In C #, Dictionary provides quick part-time-based element search. You can use it when you have many elements. It is included in the system. Collections. Generic namespace.

Before using it, you must declare its key type and value type.

Detailed description
Must contain the namespace system. collection. Generic
Each element in dictionary is a key-Value Pair (consisting of two elements: Key and value)
The key must be unique, and the value must not be unique.
Keys and values can be of any type (such as string, Int, custom type, and so on)
The time for reading a value through a key is close to O (1)
The partial order between key-value pairs cannot be defined.
Create and initialize a dictionary object
Dictionary Mydictionary = new dictionary ();
Add key
Static void main (string [] ARGs)
{
Dictionary D = new dictionary ();
D. Add ("C #", 2 );
D. Add ("C", 0 );
D. Add ("C ++",-1 );
}
Search key
Static void main (string [] ARGs)
{
Dictionary D = new dictionary ();
D. Add ("C #", 2 );
D. Add ("VB", 1 );
D. Add ("C", 0 );
D. Add ("C ++",-1 );
If (D. containskey ("VB") // true
{
Int P = d ["VB"];
Console. writeline (P );
}

If (D. containskey ("C "))
{
Int p1 = d ["C"];
Console. writeline (P1 );
}
}
Delete Element
Static void main (string [] ARGs)
{
Dictionary D = new dictionary ();
D. Add ("C #", 2 );
D. Add ("VB", 1 );
D. Add ("C", 0 );
D. Add ("C ++",-1 );

D. Remove ("C ");
D. Remove ("VB ");
}
Use containsvalue to find the existence of a value
Static void main (string [] ARGs)
{
Dictionary D = new dictionary ();
D. Add ("C #", 2 );
D. Add ("VB", 1 );
D. Add ("C", 0 );
D. Add ("C ++",-1 );
If (D. containsvalue (1 ))
{
Console. writeline ("VB ");
}
If (D. containsvalue (2 ))
{
Console. writeline ("C #");
}
If (D. containsvalue (0 ))
{
Console. writeline ("C ");
}
If (D. containsvalue (-1 ))
{
Console. writeline ("C ++ ");
}
}
Keynotfoundexception
If you try to read a key that does not exist in the dictionary, you will get a keynotfoundexception. Before reading a key, you must use containkey to check whether the key exists in the dictionary.

INT-based dictionary
Static void main (string [] ARGs)
{
Dictionary D = new dictionary ();
D. Add (1000, "planet ");
D. Add (2000, "Stars ");
// Lookup the int in the dictionary.
If (D. containskey (1000 ))
{
Console. writeline (true );
}
Console. Readline ();
}
Sorteddictionary
In the sort dictionary, the dictionary must be sorted when elements are added, so the insertion speed is slower. However, because the elements are stored in an orderly manner, binary search can be used for element search, which is more efficient.

Summary
This article briefly introduces the use of dictionary in C. Do it yourself ~

 

 

 

  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.