C # Generic Dictionary (hashtable)

Source: Internet
Author: User

C # Generic Dictionary (hashtable) using system; using system. collections. generic; public class example {public static void main () {// create a generic hash table and add the element dictionary <string, string> Oscar = new dictionary <string, string> (); Oscar. add ("Hali berry", "dance of death"); Oscar. add ("Judy danqi", "Join Hands"); Oscar. add ("nikol Kidman", "Moulin Rouge"); Oscar. add ("Jennifer Connery", "beautiful soul"); Oscar. add ("renni ziweige", "BJ singlediary"); // Delete the element Oscar. remove ("Jennifer Connery"); // assume If no element exists, add the element if (! Oscar. containskey ("Sisi · sparks") Oscar. add ("Sisi sepke", ""); // The volume and number of elements are displayed on the console. writeline ("number of elements: {0}", Oscar. count); // traverses the console of the set. writeline ("74 Oscar best actress and movie:"); foreach (keyvaluepair <string, string> kVp in Oscar) {console. writeline ("Name: {0}, movie: {1}", kVp. key, kVp. value);} // obtain the set of keys in the hash table <string, string>. keycollection keycoll = Oscar. keys; // The console that traverses the key set. writeline ("Best Actress:"); foreach (string s in keycoll) {console. writeline (s) ;}// set of hash table values <string, string>. valuecollection valuecoll = Oscar. values; // The set console that traverses values. writeline ("Best Actress movie:"); foreach (string s in valuecoll) {console. writeline (s);} // use the trygetvalue method to obtain the string slove = string corresponding to the specified key. empty; If (Oscar. trygetvalue ("Judy danqi", out slove) console. writeline ("My favorite movie {0} Judy danqi", slove); else console. writeline ("movie of Judy danqi not found"); // clear the hash table Oscar. clear (); console. readline:

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.