Introduction to common Dictionary methods in C,
Using System. Collections. Generic; // reference the namespace
// Dictionary can be understood as the public class DictionaryTest {public static void Main () {// 1. initialize Dictionary <string, string> dicA = new Dictionary <string, string> (); // 2. add element key, value-> Student ID, name dicA. add ("A01", "Zhang San"); dicA. add ("A02", "Li Si"); dicA. add ("B03", "Wang Wu"); dicA. add ("A03", "Mao"); dicA. add ("B02", "Zhang San"); // 3. delete the element dicA using the key. remove ("A03"); // 4. modify the value dicA ("A02") = "New Value"; // 5. if no element exists, add the element if (! DicA. containsKey ("A08") dicA. add ("A08", "Haha"); // 6. obtain the number of elements int count = dicA. count; // 7. traverse the set foreach (KeyValuePair <string, string> kvp in dicA) {Console. writeLine ("student ID: {0}, name: {1}", kvp. key, kvp. value);} // 8. dictionary <string, string>. keyCollection allKeys = dicA. keys; Console. writeLine ("Best Actress:"); foreach (string oneKey in allKeys) {Console. writeLine (oneKey);} // Dictionary <string, string>. valueCollection allValues = dicA. values; // foreach (string oneValue in allValues) // {// Console. writeLine (oneValue); //} // 9. value: string stuName = dicA ("A01"); // 10. clear dicA. clear ();}}