学用 ASP.Net 之 System.Collections.Specialized.CollectionsUtil 类 通过 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); }