DotNet中的集合对象(2): Hashtable
Hashtable(哈希表)也是一个可变长的数组,表中的每个项目可以通过关键字Key值来访问.关键字一般是某个字符串,也可以是其它类型的对象.每个元素本身不是唯一的,但每个元素本身必需有一个惟一的关键字.
using System;
using System.Collections;
class MyClass
{
private string m_String;
public MyClass(string s)
{
m_String = s;
}
public string Str
{
get
{
return m_String;
}
}
}
class TestHashtable
{
static void Main()
{
Hashtable hash = new Hashtable();
MyClass mc1 = new MyClass("hl");
hash.Add(mc1, mc1);
MyClass c = hash[mc1] as MyClass;
if (c != null)
{
Console.WriteLine(c.Str);
}
else
{
Console.WriteLine("error");
}
Console.Read();
}
}
Hashtable主要的作用是可以快速访问一个大而无序的记录表.
注意:
Hashtable元素的访问也必须转换成合适的类型.
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
Hashtable主要的作用是可以快速访问一个大而无序的记录表.
注意:
Hashtable元素的访问也必须转换成合适的类型.
如果您喜欢本文,更多文章,欢迎订阅我的微信订阅号:open_dotNET