一.基礎篇
1.Dictionary泛型類提供了從一組鍵到一組值的映射,即鍵和值的集合類。
2.Dictionary通過鍵來檢索值的速度是非常快的,這是因為 Dictionary 類是作為一個哈希表來實現的。
3.定義方式:
Dictionary<[Key], [Value]> openWith = new Dictionary<[Key], [Value]>();
其中:Key代表此泛型類的鍵,不可重復。
Value代表此泛型類中鍵對應的值。
Key和Value可以用int,decimal,float等值類型,也可以用string,class等引用類型。
舉例:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication1 { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Test(); } public void Test() { //Key為值類型 Value為值類型 Dictionary<int, int> dicInt = new Dictionary<int, int>(); //Key為值類型 Value為引用類型 Dictionary<int, string> dicString = new Dictionary<int, string>(); //Key為引用類型 Value為引用類型 Dictionary<TestInfo, TestInfo> dicTestClass = new Dictionary<TestInfo, TestInfo>(); } } public class TestInfo { public string ID { get; set; } public string Name { get; set; } public string Pwd { get; set; } } }View Code
4.添加鍵值對的方式:
①Dictionary.Add(Key,Value)方式:
//Key為值類型 Value為引用類型 Dictionary<int, string> dicString = new Dictionary<int, string>(); dicString.Add(0,"00001"); dicString.Add(1, "00002"); dicString.Add(2, "00003"); dicString.Add(3, "00004");View Code
②Dictionary[Key]=Value 方式:
//Key為值類型 Value為引用類型 Dictionary<int, string> dicString = new Dictionary<int, string>(); dicString[0] = "00001"; dicString[1] = "00002"; dicString[2] = "00003"; dicString[3] = "00004";View Code
③兩種方式對比:
相同:二者皆可添加鍵值對。
差異:第一種方式,當鍵不存在,相當於插入此鍵值對,當插入重復鍵時,則會引發ArgumentException類型的異常。
第二種方式,當鍵不存在,相當於插入此鍵值對,當鍵存在時,相當於修改該鍵對應的值;當Dictionary[Key]取值
時,如果此Key不存在,則會引發KeyNotFoundException異常。
總結:添加鍵值對,推薦以第二種為主。
二.進階篇
1.ContainsKey(Key)判斷鍵中是否含有此Key
//Key為值類型 Value為引用類型 Dictionary<int, string> dicString = new Dictionary<int, string>(); dicString[0] = "00001"; dicString[1] = "00002"; dicString[2] = "00003"; dicString[3] = "00004"; if (dicString.ContainsKey(4)) { Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('存在該鍵');</script>"); } else { Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('不存在該鍵');</script>"); }View Code
2.ContainsValue(Value)判斷值中是否含有此Value
//Key為值類型 Value為引用類型 Dictionary<int, string> dicString = new Dictionary<int, string>(); dicString[0] = "00001"; dicString[1] = "00002"; dicString[2] = "00003"; dicString[3] = "00004"; if (dicString.ContainsValue("00004")) { Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('存在該值');</script>"); } else { Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('不存在該值');</script>"); }View Code
3.Foreach和KeyValuePair<Key, Value>配合取出所有鍵值對
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //Key為值類型 Value為引用類型 Dictionary<int, string> dicString = new Dictionary<int, string>(); dicString[0] = "00001"; dicString[1] = "00002"; dicString[2] = "00003"; dicString[3] = "00004"; foreach (KeyValuePair<int, string> item in dicString) { Console.WriteLine("Key={0},Value={1}",item.Key,item.Value); } Console.ReadKey(); } } }View Code
4.Dictionary.Keys,Dictionary.Values和foreach配合取出所有key,所有value。(也可以利用第三步中單獨取key和value)
//取出所有key foreach (int key in dicString.Keys) { Console.WriteLine("Key={0}", key); } //取出所有value foreach (string value in dicString.Values) { Console.WriteLine("Value={0}",value); }View Code
5.Dictionary.Clear()和Dictionary.Remove(Key)移除鍵值對
//移除所有的鍵和值 dicString.Clear(); //移除鍵為0的鍵值對 dicString.Remove(0);View Code
6.Dictionary.Reverse()進行反轉序列輸出
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //Key為值類型 Value為引用類型 Dictionary<int, string> dicString = new Dictionary<int, string>(); dicString[0] = "00001"; dicString[1] = "00002"; dicString[2] = "00003"; dicString[3] = "00004"; //原始排序輸出 foreach (KeyValuePair<int,string> item in dicString) { Console.WriteLine("Key={0},Value={1}",item.Key,item.Value); } //進行反轉排序,存入iList IEnumerable<KeyValuePair<int,string>> iList = dicString.Reverse(); Console.WriteLine("-----------------------------------------"); //反轉輸出 foreach (KeyValuePair<int, string> item in iList) { Console.WriteLine("Key={0},Value={1}", item.Key, item.Value); } Console.ReadKey(); //輸出結果如下: /* Key=0,Value=00001 Key=1,Value=00002 Key=2,Value=00003 Key=3,Value=00004 ------------------------------------------------ Key=3,Value=00004 Key=2,Value=00003 Key=1,Value=00002 Key=0,Value=00001 */ } } }View Code