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 ~
- Using system;
- Using system. Collections. Generic;
- Class dictionarydemo
- {
- Static void main (string [] ARGs)
- {
- Dictionarydemo001 ();
- Console. Readline ();
- Dictionarydemo002 ();
- Console. Readline ();
- Dictionarydemo003 ();
- Console. Readline ();
- }
- /// <Summary>
- /// General Usage
- /// </Summary>
- Static void dictionarydemo001 ()
- {
- Dictionary <int, string> dict = new dictionary <int, string> ();
- Dict. Add (1, "111 ");
- Dict. Add (2, "222 ");
- // Determine whether the corresponding key exists and display it
- If (dict. containskey (2 ))
- {
- Console. writeline (dict [2]);
- }
- // Traverse keys
- Foreach (VAR item in dict. Keys)
- {
- Console. writeline ("key: {0}", item );
- }
- // Traverse values
- Foreach (VAR item in dict. values)
- {
- Console. writeline ("value: {0}", item );
- }
- // Traverse the entire dictionary
- Foreach (VAR item in dict)
- {
- Console. writeline ("key: {0} value: {1}", item. Key, item. value );
- }
- }
- /// <Summary>
- /// The parameter is of another type
- /// </Summary>
- Static void dictionarydemo002 ()
- {
- Dictionary <string, string []> dict = new dictionary <string, string []> ();
- Dict. Add ("1", "1, 11, 111". Split (','));
- Dict. Add ("2", "2,22, 222". Split (','));
- Console. writeline (dict ["2"] [2]);
- }
- /// <Summary>
- /// Call a custom class
- /// </Summary>
- Static void dictionarydemo003 ()
- {
- Dictionary <int, yongfa365> dict = new dictionary <int, yongfa365> ();
- For (INT I = 0; I <10; I ++)
- {
- Yongfa365 y = new yongfa365 ();
- Y. usercode = I;
- Y. Username = "www.yongfa365.com" + I. tostring ();
- Dict. Add (I, y );
- }
- Foreach (VAR item in dict)
- {
- Console. writeline ("{0} One: {1} Username: {2}", item. Key, item. value. usercode, item. value. username );
- }
- }
- }
- Class yongfa365
- {
- Public int usercode {Get; set ;}
- Public String username {Get; set ;}
- }