1. Initialize a key-value pair
1 // initialize to define a key-value pair, note the last parenthesis 2 dictionary<intstringnew dictionary<intstring> ();
2. Adding elements to key-value pairs
1 //the Add function of a key-value pair2Dic.ADD(2,"Zhao");3Dic.ADD(4,"Qian");4Dic.ADD(6,"Sun");5Dic.ADD(8,"Li");
3. ContainsKey and Containsvalue functions for key-value pairs
1 //ContainsKey and Containsvalue functions for key-value pairs2Console.WriteLine (dic[4]);3Console.WriteLine ("whether key-value pairs contain key 2:"+ DIC.ContainsKey(2));4Console.WriteLine ("whether key-value pairs contain key 5:"+ DIC.ContainsKey(5));5Console.WriteLine ("whether a key-value pair contains a value Zhao:"+ DIC.Containsvalue("Zhao"));6Console.WriteLine ("whether a key-value pair contains a value Zhao:"+ DIC.Containsvalue("Zhao"));
4. Number of key-value pairs
1 // number of key-value pairs 2 Console.WriteLine (" key-value pairs contain the number of key values:" + dic. ) Count);
5. Keys property for key-value pairs
1 // Keys property for key-value pairs 2 foreach (intitem in dic.) Keys)3{4 Console.WriteLine (" key: {0}, Value: {1} "Item, Dic[item]); 5 }
6. Traversing a key-value pair
1 // To traverse a key-value pair 2 foreach (keyvaluepair<int,string in dic) 3 {4 Console.WriteLine (The key is: {0}, the value is: {1}"kv. Key, KV. Value); 5 }
C # key-value pairs