通過 CollectionsUtil 建立或封裝的 "鍵/值對" 類(實現 IDictionary 的), 可以忽略 Key 的大小寫.
主要成員:
/* 靜態方法 */CollectionsUtil.CreateCaseInsensitiveHashtable(); //建立或封裝 Hashtable 等, 可初始化容量CollectionsUtil.CreateCaseInsensitiveSortedList(); //建立有序的雜湊表 SortedList
建立忽略大小寫 Hashtable:
protected void Button1_Click(object sender, EventArgs e){ Hashtable hash = CollectionsUtil.CreateCaseInsensitiveHashtable(); //這就建立了一個忽略大小寫雜湊表 hash["KEY1"] = 123; int n = (int)hash["key1"]; //123 TextBox1.Text = n.ToString(); try { hash.Add("Key1", 456); } catch (Exception err) { Response.Write(err.Message); } //已添加項。字典中的關鍵字:“KEY1”所添加的關鍵字:“Key1” }
建立忽略大小寫 SortedList:
protected void Button1_Click(object sender, EventArgs e){ SortedList sl = CollectionsUtil.CreateCaseInsensitiveSortedList(); //這就建立了一個忽略大小寫 SortedList sl["KEY1"] = 123; TextBox1.Text = sl["key1"].ToString(); //123 try { sl.Add("Key1", 456); } catch (Exception err) { Response.Write(err.Message); } //已添加項。字典中的關鍵字:“KEY1”所添加的關鍵字:“Key1” }
封裝一個 Hashtable 為忽略大小寫:
protected void Button1_Click(object sender, EventArgs e){ Hashtable hash = new Hashtable(); hash.Add("KEY1", "aaa"); hash.Add("KEY2", "bbb"); hash.Add("KEY3", "ccc"); bool b1 = hash.Contains("KEY1"); //True bool b2 = hash.Contains("key1"); //False hash = CollectionsUtil.CreateCaseInsensitiveHashtable(hash); bool b3 = hash.Contains("key1"); //True TextBox1.Text = string.Concat(b1, "\n", b2, "\n", b3);}