欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > asp.net >内容正文

asp.net

学用 ASP.Net 之 System.Collections.Specialized.CollectionsUtil 类

发布时间:2025/3/20 asp.net 35 豆豆
生活随笔 收集整理的这篇文章主要介绍了 学用 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"]; //123TextBox1.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(); //这就建立了一个忽略大小写的 SortedListsl["KEY1"] = 123;TextBox1.Text = sl["key1"].ToString(); //123try { 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"); //Truebool b2 = hash.Contains("key1"); //Falsehash = CollectionsUtil.CreateCaseInsensitiveHashtable(hash);bool b3 = hash.Contains("key1"); //TrueTextBox1.Text = string.Concat(b1, "\n", b2, "\n", b3); }

总结

以上是生活随笔为你收集整理的学用 ASP.Net 之 System.Collections.Specialized.CollectionsUtil 类的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。