C # key-value pairs

Source: Internet
Author: User

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

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.