C # dictionary usage Summary
1. Usage 1: regular use
Before adding a key-value pair, you must determine whether the key exists. If the key already exists and is not determined, an exception is thrown.Therefore, it is difficult to judge every time. An extension method is used in the remarks.
Public static void dicsample1 () {dictionary <string, string> plist = new dictionary <string, string> (); try {If (plist. containskey ("Item1") = false) {plist. add ("Item1", "Zhejiang");} If (plist. containskey ("item2") = false) {plist. add ("item2", "Shanghai");} else {plist ["item2"] = "Shanghai";} If (plist. containskey ("item3") = false) {plist. add ("item3", "Beijiang") ;}} catch (system. exception e) {console. writeline ("error: {0}", E. message);} // determine whether the corresponding key exists and display if (plist. containskey ("Item1") {console. writeline ("output:" + plist ["Item1"]);} // traverses key foreach (var key in plist. keys) {console. writeline ("output key: {0}", key) ;}// traverse value foreach (string value in plist. values) {console. writeline ("output value: {0}", value);} // traverses key and value foreach (var dic in plist) {console. writeline ("output key: {0}, value: {1}", DIC. key, DIC. value );}}
2. Usage 2: The value of dictionary is an array.
/// <Summary> // The value of dictionary is an array // </Summary> Public static void dicsample2 () {dictionary <string, string []> DIC = new dictionary <string, string []> (); string [] Zhejiang = {"Huzhou", "Hangzhou", "Taizhou "}; string [] Shanghai = {"budong", "buxi"}; dic. add ("ZJ", Zhejiang); dic. add ("sh", Shanghai); console. writeline ("output:" + DIC ["ZJ"] [0]);}
3. Usage 3: The value of dictionary is a class.
// The value of dictionary is a class public static void dicsample3 () {dictionary <string, student> stulist = new dictionary <string, student> (); Student Stu = NULL; for (INT I = 0; I <3; I ++) {Stu = new student (); Stu. name = I. tostring (); Stu. name = "stuname" + I. tostring (); stulist. add (I. tostring (), Stu);} foreach (VAR student in stulist) {console. writeline ("output: Key {0}, num: {1}, name {2}", student. key, student. value. name, student. value. name );}}
Student class:
Public class student {Public String num {Get; set;} public string name {Get; Set ;}}
4. usage of the dictionary Extension Method
/// <Summary> /// usage of the dictionary Extension Method /// </Summary> Public static void dicsample4 () {// 1) common call dictionary <int, string> dict = new dictionary <int, string> (); dictionaryextensionmethodclass. tryadd (dict, 1, "zhangsan"); dictionaryextensionmethodclass. tryadd (dict, 2, "wangwu"); dictionaryextensionmethodclass. addorpeplace (dict, 3, "wangwu"); dictionaryextensionmethodclass. addorpeplace (dict, 3, "zhangwu"); dictionaryextensionmethodclass. tryadd (dict, 2, "Lisi"); // 2) tryadd and addorreplace methods have strong self-describing capabilities, which are easy to use and simple: dict. addorpeplace (20, "orange"); dict. tryadd (21, "banana"); dict. tryadd (22, "apple"); // 3) connect to dict like LINQ or jquery. tryadd (10, "Bob "). tryadd (11, "Tom "). addorpeplace (12, "Jom"); // 4) obtain the value string F = "ba"; dict. trygetvalue (31, out f); console. writeline ("F: {0}", f); foreach (var dic in dict) {console. writeline ("output: Key: {0}, value: {1}", DIC. key, DIC. value);} // 5) Use getvalue to obtain the value var V1 = dict. getvalue (111, null); var v2 = dict. getvalue (10, "ABC"); // 6) add var dict1 = new dictionary <int, int> (); dict1.addorpeplace (3, 3); dict1.addorpeplace (5, 5); var dict2 = new dictionary <int, int> (); dict2.addorpeplace (1, 1); dict2.addorpeplace (4, 4); dict2.addrange (dict1, false );}
class of the Extension Method
Public static class dictionaryextensionmethodclass {// <summary> /// try to add the key and value to the dictionary: if it does not exist, add it; yes, do not add or export the regular // </Summary> Public static dictionary <tkey, tvalue> tryadd <tkey, tvalue> (this dictionary <tkey, tvalue> dict, tkey key, tvalue) {If (dict. containskey (key) = false) dict. add (Key, value); Return dict;} // <summary> // Add or replace the key and value with the dictionary. If not, add or replace the key and value with the dictionary, replace /// </Summary> Public static Diction Ary <tkey, tvalue> addorpeplace <tkey, tvalue> (this dictionary <tkey, tvalue> dict, tkey, tvalue value) {dict [Key] = value; return dict ;} /// <summary> /// obtain the value associated with the specified key. If no value exists, the default value of the input is returned. /// </Summary> Public static tvalue getvalue <tkey, tvalue> (this dictionary <tkey, tvalue> dict, tkey key, tvalue defaultvalue) {return dict. containskey (key )? Dict [Key]: defaultvalue ;} /// <summary> /// add key-value pairs to the dictionary in batches /// </Summary> /// <Param name = "replaceexisted"> if it already exists, replace </param> Public static dictionary <tkey, tvalue> addrange <tkey, tvalue> (this dictionary <tkey, tvalue> dict, ienumerable <keyvaluepair <tkey, tvalue> values, bool replaceexisted) {foreach (VAR item in values) {If (dict. containskey (item. key) = false | replaceexisted) dict [item. key] = item. value ;}return dict ;}}
References