Dictionary<t,t> Key-value pairs collection (dictionary)

Source: Internet
Author: User

<1>

Using system;using system.collections.generic;using system.linq;using system.text;namespace Dictionary Dictionary {class program {static void Main (string[] args) {//hashtable key-value pair Collection declaration: Hashtable HT = new HASHTABL  E (); In the process of declaring a//dictionary key value that does not determine the type of the key and value, the collection differs from the Hashtable key-value pair collection, and the type of the Dictionary key and value has been declared to determine the Dictionary<int , string> dic = new Dictionary<int, string> (); Creates a key-value pair for the collection object, with the key type int.            The type of the value is string. Dic.            ADD (1, "Zhang San"); Dic.            ADD (2, "John Doe");            D.add ("Zhang San", 28) error, because Dictionary<int,string> has determined that its key can only be of type int, the value can only be a string type. Dic. ADD (1, "New Zhang San");//error. The key is unique.            A key value of 1 has been added to the previous one. DIC[1] = "New Zhang San"; This will not be an error. In fact, here is a judgment. That is: If the DIC key value pair has a key value of 1, then it will be updated to "new Zhang San". If there are no key-value pairs for key 1. Then add a key value of 1 for the "New Zhang San" value to//dic. Clear ();            Clears all the elements of DIC, the key-value pair collection. The Hashtable key-value pair collection can only be traversed in this way.            That is, you can traverse keys only. foreach (var item in DIC. Keys)//traverse DIC for all key-value pairs {Console.WriteLine (item);            Output Console.WriteLine ("key is: {0}----value is: {1}", item, Dic[item]);//output: Key is: 1----value is: Zhang Sanxi is: 2----value is: John Doe            }//and dictionary key-value pairs in addition to the above way to traverse, you can also use this generally to traverse.            foreach (Keyvaluepair<int, string> item in DIC)//The DIC key pair is traversed in the form of key-value pairs (a pair of pairs). {Console.WriteLine (item);//output: [1, Zhang San] [2, John Doe] Console.WriteLine ("Key is: {0}----value is: {1}", item. Key, item. value);//output: The key is: 1----value is: Zhang Sanxi is: 2----value is: John Doe}//dictionary key-value pair collection instance: each letter of the STR string as key, each letter out The current number of times as a value.            Added to the key-value pair object.            String str = "Welcome to China";            Dictionary<char, int> dic2 = new Dictionary<char, int> ();            int i = 1; foreach (var item in str)//Traverse str This string {if (item = = ')//If the currently traversed character is ' ' Skip the current loop.                That is: Remove the space {continue;//Skip the current loop and proceed to the next round of loops. } if (Dic2. Keys.contains (item))//If DIC2 this key-value pair contains keyItem This key value pair {dic2[item]++; The value of the key value pair will be increased from the original base. That is, +1} else {Dic2. ADD (item, 1); Otherwise, use item as key. 1 as the default value.                Added to Dic2 in this key-value pair. }} foreach (var item in Dic2) {Console.WriteLine ("Letter: {0}----occurrences: {1} times" , item. Key, item.            Value);        } console.readkey (); }    }}


Dictionary<t,t> Key-value pairs collection (dictionary)

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.