1.創建泛型哈希表,然后加入元素
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.遍歷key
foreach (string key in openWith.Keys) { Console.WriteLine("Key = {0}", key); }
3.遍歷value
foreach (string value in openWith.Values) { Console.WriteLine("value = https://www.cnblogs.com/qingchengshiguang/p/{0}", value); }
4.遍歷value, Second Method
Dictionary<string, string>.ValueCollection valueColl = openWith.Values; foreach (string s in valueColl) { Console.WriteLine("Second Method, Value = https://www.cnblogs.com/qingchengshiguang/p/{0}", s); }
5.遍歷字典
foreach (KeyValuePair<string, string> kvp in openWith) { Console.WriteLine("Key = {0}, Value = https://www.cnblogs.com/qingchengshiguang/p/{1}", kvp.Key, kvp.Value); }
6.添加存在的元素
try { openWith.Add("txt", "winword.exe"); } catch (ArgumentException) { Console.WriteLine("An element with Key = \"txt\" already exists."); }
7.洗掉元素
openWith.Remove("doc"); if (!openWith.ContainsKey("doc")) { Console.WriteLine("Key \"doc\" is not found."); }
8.判斷鍵存在
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]);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/80515.html
標籤:C#
上一篇:MD5加密
下一篇:C#通過檔案路徑獲取檔案名
