學用 ASP.Net 之 System.Collections.Specialized.CollectionsUtil 類

來源:互聯網
上載者:User
通過 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);}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.