DictionaryEntry 是包含 Key / Value 一对值的简单结构;
Hashtable(哈希表)是一组 Key / Value 的集合, 准确地讲是一组 DictionaryEntry 的集合.
DictionaryEntry 简例:
protected void Button1_Click(object sender, EventArgs e) { DictionaryEntry d1; d1.Key = "k1"; d1.Value = 123; DictionaryEntry d2 = new DictionaryEntry("k2", 456); TextBox1.Text = string.Concat(d1.Key, ":", d1.Value, "; ", d2.Key, ":", d2.Value); //k1:123; k2:456 }
Hashtable 成员:
/* 静态方法 */ Hashtable.Synchronized(); // /* 属性 */ Count //元素数 IsFixedSize // IsReadOnly // IsSynchronized // Keys //键集合 SyncRoot // Values //值集合 /* 方法 */ Add() //添加 Clear() //清空 Clone() // Contains() //是否包含指定 Key, 同 ContainsKey() ContainsKey() //是否包含指定 Key ContainsValue() //是否包含指定 Value CopyTo() //复制到 Equals() // GetEnumerator() // GetObjectData() // OnDeserialization() // Remove() //移除 /* 扩展方法 */ AsParallel() // AsQueryable() // Cast<>() // OfType<>() //
入手练习:
protected void Button1_Click(object sender, EventArgs e) { Hashtable hash = new Hashtable(); hash.Add("k1", "AAAAA"); hash.Add("k2", "BBBBB"); hash.Add("k3", "CCCCC"); hash.Add("k4", "DDDDD"); int n1 = hash.Count; //4 string s1 = hash["k2"].ToString(); //BBBBB hash["k2"] = "12345"; string s2 = hash["k2"].ToString(); //12345 hash.Remove("k2"); int n2 = hash.Count; //3 hash.Clear(); int n3 = hash.Count; //0 TextBox1.Text = string.Concat(n1, "\n", s1, "\n", s2, "\n", n2, "\n", n3); }
遍历:
protected void Button1_Click(object sender, EventArgs e) { Hashtable hash = new Hashtable(); hash.Add(1, "AAAAA"); hash.Add(2, "BBBBB"); hash.Add(3, "CCCCC"); hash.Add(4, "DDDDD"); string str = ""; foreach (DictionaryEntry de in hash) { str += string.Format("{0} : {1}\n", de.Key, de.Value); } TextBox1.Text = str; } /*遍历结果: 4 : DDDDD 3 : CCCCC 2 : BBBBB 1 : AAAAA **********/
Contains()、ContainsKey()、ContainsValue():
protected void Button1_Click(object sender, EventArgs e) { Hashtable hash = new Hashtable(); hash.Add("k1", 123); hash.Add("k2", "ABC"); hash.Add("k3", 3.14); hash.Add("k4", null); bool b1 = hash.Contains("k1"); //True bool b2 = hash.Contains("k4"); //True bool b3 = hash.Contains("k5"); //False bool b4 = hash.ContainsKey("k1"); //True bool b5 = hash.ContainsKey("k4"); //True bool b6 = hash.ContainsKey("k5"); //False bool b7 = hash.ContainsValue("3.14"); //False bool b8 = hash.ContainsValue(3.14); //True bool b9 = hash.ContainsValue(null); //True bool b0 = hash.ContainsValue(""); //False TextBox1.Text = string.Format("{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}\n{7}\n{8}\n{9}", b1, b2, b3, b4, b5, b6, b7, b8, b9, b0 ); } protected void Button2_Click(object sender, EventArgs e) { Hashtable hash = new Hashtable(); if (!hash.Contains("k1")) { hash.Add("k1", DateTime.Now); } if (hash.Contains("k1")) { hash.Remove("k1"); } }
Keys、Values:
protected void Button1_Click(object sender, EventArgs e) { Hashtable hash = new Hashtable(); hash.Add("k1", "AAA"); hash.Add("k2", "BBB"); hash.Add("k3", "CCC"); hash.Add("k4", "DDD"); ICollection ks = hash.Keys; ICollection vs = hash.Values; string s1 = "", s2 = ""; foreach (string k in ks) { s1 += k + "; "; } //k4; k1; k2; k3; foreach (string v in vs) { s2 += v + "; "; } //DDD; AAA; BBB; CCC; TextBox1.Text = s1 + "\n" + s2; }
分类:
C# 与 Net
, StrUtils 单元
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
2010-01-05 RegularExpressions(1) 一个 Net 样式的、适用于 Delphi 2009 之后版本的正则表达式组件
2008-01-05 Delphi 中的 XMLDocument 类详解(16) - 节点列表中的第一个与最后一个节点
2008-01-05 Delphi 中的 XMLDocument 类详解(15) - 创建与保存 xml