c#中如何使用hashtable

来源:百度知道 编辑:UC知道 时间:2024/06/18 06:23:02
c#中如何使用hashtable 如何读hashtable里面的值

// 创建 Hashtable 实例
System.Collections.Hashtable ht = new System.Collections.Hashtable();

// 赋值
ht["UserName"] = "csharpxml";
ht["Password"] = "123456789";

// 或者
ht.Add("age", 32);

// 读取
// Asp.net
Response.Write(ht["UserName"] + "<br />" + ht["Password"] + "<br />" + ht["age"]);

// WinForm
MessageBox.Show(ht["UserName"].ToString() + "\r\n" + ht["Password"].ToString() + "\r\n" + ((int)ht["age"]).ToString());

HashTable在System.Collections的命名空间下。
是个根据object索引键访问object值的集合。
没有泛型,使用的时候只能进行强制转换。
Dictionary只能通过string类来定义键值,
如Dictionary d;d["wang"]="王";
CollectionBase只能通过数字索引来访问,
如CollectionBase col;col[0]="好";
HashTable比起上述两种类型来说有更强的访问能力。如:
HastTable hs