1.建立泛型哈希表,而後加入元素 spa
Dictionary<string,string> openWith=new Dictionary<string, string>(); openWith.Add("txt","notepad.exe"); openWith.Add("bmp","paint.exe"); openWith.Add("dib","paint.exe"); openWith.Add("rtf","wordpad.exe");
2.遍歷keycode
foreach (string key in openWith.Keys) { Console.WriteLine("Key = {0}", key); }
3.遍歷valueblog
foreach (string value in openWith.Values) { Console.WriteLine("value = {0}", value); }
4.遍歷value, Second Methodelement
Dictionary<string, string>.ValueCollection valueColl = openWith.Values; foreach (string s in valueColl) { Console.WriteLine("Second Method, Value = {0}", s); }
5.遍歷字典string
foreach (KeyValuePair<string, string> kvp in openWith) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); }
6.添加存在的元素it
try { openWith.Add("txt", "winword.exe"); } catch (ArgumentException) { Console.WriteLine("An element with Key = \"txt\" already exists."); }
7.刪除元素io
openWith.Remove("doc"); if (!openWith.ContainsKey("doc")) { Console.WriteLine("Key \"doc\" is not found."); }
8.判斷鍵存在class
if (openWith.ContainsKey("bmp")) // True { Console.WriteLine("An element with Key = \"bmp\" exists."); }
9.參數爲其它類型泛型
Dictionary<int, string[]> OtherType = new Dictionary<int, string[]>(); OtherType.Add(1, "1,11,111".Split(',')); OtherType.Add(2, "2,22,222".Split(',')); Console.WriteLine(OtherType[1][2]);