Introduction to common Dictionary methods in C,

Source: Internet
Author: User
Tags allkeys

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 ();}}

  

Related Article

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.