Dictionary
Dictionary can replace the Hashtable;
There is a new way to traverse
foreach(KeyValuePair <int,stringin dic){}
Dictionary Practice
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.threading.tasks;namespace Dictionary Dictionary {class Program {Static voidMain (string[] args) {dictionary<int,string> dic =Newdictionary<int,string> (); Dic. ADD (1,"Jing Ke"); Dic. ADD (2,"Niezheng"); Dic. ADD (3,"Exclusive"); Dic. ADD (4,"close to the");foreach(intIteminchDic. Keys) {Console.WriteLine ("{0} corresponding value is {1}", Item,dic[item]); }foreach(keyvaluepair<int,string> PairinchDIC) {Console.WriteLine ("{0} corresponding value is {1}", pair. Key,pair. Value); } console.readkey (); } }}
Detecting character occurrences with dictionary
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.threading.tasks;namespace Dictionary detect letter Occurrences {class Program {Static voidMain (string[] args) {//detect the number of occurrences of each character in the string "Welcome to China";dictionary<Char,int> dic =Newdictionary<Char,int> ();stringstr ="Welcome to China"; for(inti =0; I < Str. Length; i++) {if(str[i]=="') {Continue; }if(DIC. ContainsKey (str. ToLower ()) [i]) {dic[(str). ToLower ()) [i]]++; }Else{dic[(str. ToLower ()) [i]] =1; } }foreach(keyvaluepair<Char,int> IteminchDIC) {Console.WriteLine ("{0} has occurred {1} times", item. Key,item. Value); } console.readkey (); } }}
Dictionary dictionary for C #