- Dictionary
- Dictionary is a collection of stored keys and values
- Dictionary are unordered, key keys are unique
- When using, we first introduce the generic collection namespace using System.Collections.Generic;
- Create a Dictionary object
- Dictionary<key, value> dic = new Dictionary<key, value> ();
- dictionary<string, int> dic = new dictionary<string, int> ();
- Add method for adding a key-value pair
- Dic. Add ("Xiao Ming", 13);
- Dic. ADD ("Little Red", 15);
- Using the Remove () method, set the key value to remove the value of the key that contains the value
- Dic. Remove ("Little Red");
- Clear () empties all content in the current dictionary
- Count gets the number of value in the current dictionary
- int count = dic. Count;
- Console.WriteLine ("+ Count +" key value pairs in the current dictionary!) ");
- ContainsKey () method to see if the current dictionary contains key-"Xiao Ming"
- BOOL B = dic. ContainsKey ("Xiao Ming");
- if (b)
- {
- Console.WriteLine ("There is the age of Xiaoming!" ");
- }else
- {
- Console.WriteLine ("There is no age for xiaoming!") ");
- }
- The Containsvalue () method directly checks to see if the current value exists
- BOOL B2 = dic. Containsvalue (10);
- if (B2)
- {
- Console.WriteLine ("The Age of 10 people!") ");
- }
- Else
- {
- Console.WriteLine ("There is no age of 10!") ");
- }
- TryGetValue () method, attempting to get the value corresponding to the specified key
- int s;
- BOOL BB = dic. TryGetValue ("Xiaoming", out s);
- If the current dictionary contains the key "Xiaoming", then the corresponding value is obtained and saved in S, bb=true
- If the current dictionary does not contain the key "Xiaoming", then S=null, bb = false;
- Get value from Key
- int age = dic["Xiao Ming"];
- Console.WriteLine ("Xiaoming's age is:" + ages);
-
1 usingSystem;2 //Introducing generic Collection namespaces3 usingSystem.Collections.Generic;4 5 namespaceDictionarydemo6 {7 class Program8 {9 Static voidMain (string[] args)Ten { One //Create a Dictionary object, the type of key is string, the type of value is int Adictionary<string,int> dic =Newdictionary<string,int>(); - //Add method for adding a key-value pair -Dic. ADD ("Xiao Ming", -); theDic. ADD ("Little Red", the); - - - //using the Remove () method, set the key value to remove the value of the key that contains the value +Dic. Remove ("Little Red"); - + //Clear () empties all content in the current dictionary A DiC. Clear (); at - - //Count Gets the number of value in the current dictionary - intCount =DiC. Count; -Console.WriteLine ("the current dictionary has"+ Count +"a key value pair! "); - in //ContainsKey () method to see if the current dictionary contains key-"Xiao Ming" - BOOLb = dic. ContainsKey ("Xiao Ming"); to if(b) + { -Console.WriteLine ("There is the age of xiaoming! "); the } * Else $ {Panax NotoginsengConsole.WriteLine ("There is no age for xiaoming! "); - } the + //the Containsvalue () method directly checks to see if the current value exists A BOOLb2 = dic. Containsvalue (Ten); the if(B2) + { -Console.WriteLine ("There is an age of 10 people! "); $ } $ Else - { -Console.WriteLine ("There are no people aged 10! "); the } - Wuyi //TryGetValue () method, attempting to get the value corresponding to the specified key the ints; - BOOLbb = dic. TryGetValue ("xiaoming", outs); Wu //If the current dictionary contains the key "Xiaoming", then the corresponding value is obtained and saved in S, Bb=true - //If the current dictionary does not contain the key "Xiaoming", then S=null, bb = false; About if(BB) $ { -Console.WriteLine ("contains "Xiao Ming" this key"); - } - Else A { +Console.WriteLine ("does not contain "Xiao Ming" this key! "); the } - $ //get value from Key the intAge = dic["Xiao Ming"]; theConsole.WriteLine ("Xiaoming's age is:"+Age ); the } the } -}
"Learning notes" C # Dictionary