看文章时,看到Hashtable,就把Hashtable的使用方法总结了一下,下面是代码和说明:
/一 哈希表(Hashtable)简述
// 在.NET Framework中,Hashtable是System.Collections命名空间提供的一个容器,
//用于处理和表现类似keyvalue的键值对,其中key通常可用来快速查找,
//同时key是区分大小写的;value用于存储对应于key的值。
//Hashtable中keyvalue键值对均为object类型,所以Hashtable可以支持任何类型的keyvalue键值对。
//每个元素都是一个存储在 DictionaryEntry 对象中的键/值对。键不能为空引用(在 Visual Basic中为Nothing),但值可以。
//二 哈希表的简单操作
// 公共方法
// 在哈希表中添加一个keyvalue键值对: HashtableObject.Add(key,value);
// 在哈希表中移除带有指定键的元素: HashtableObject.Remove(key);
// 从哈希表中移除所有元素: HashtableObject.Clear();
// 判断哈希表是否包含特定键key: HashtableObject.Contains(key);
// 判断哈希表是否包含特定键key: HashtableObject.ContainsKey(key);
// 判断哈希表是否包含特定值value: HashtableObject.ContainsValue(value);
// 获取当前实例的类型: HashtableObject.GetType();
//公共属性
// 获取包含Hashtable 中的键的ICollection: HashtableObject. Keys
// 获取包含Hashtable 中的值的ICollection: HashtableObject. Values
// 获取包含在Hashtable 中的键/值对的数目: HashtableObject. Count
namespace HashtableExample
{
class Program
{
static void Main(string[] args)
{
Hashtable openWith = new Hashtable();
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
Console.WriteLine();
Console.WriteLine("Hashtable 中的键/值对的数目={0}",openWith.Count);
Console.WriteLine(openWith.GetType());
// key不能相同,但value可以相同
// The Add method throws an exception if the new key is already in the hash table.
try
{
openWith.Add("txt", "winword.exe");
}
catch
{
Console.WriteLine("An element with Key = \"txt\" already exists.");
}
Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]);
openWith["rtf"] = "winword.exe";
Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]);
openWith["doc"] = "word.exe";
try
{
Console.WriteLine("For key = \"doc\", value = {0}.", openWith["doc"]);
}
catch
{
Console.WriteLine("Key = \"doc\" is not found.");
}
try
{
Console.WriteLine("For key = \"tif\", value = {0}.", openWith["tif"]);
}
catch
{
Console.WriteLine("Key = \"tif\" is not found.");
}
//key是区分大小写的
if (!openWith.ContainsKey("ht"))
{
openWith.Add("ht", "hypertrm.exe");
Console.WriteLine("Value added for key = \"ht\": {0}", openWith["ht"]);
}
if (!openWith.ContainsKey("Ht"))
{
openWith.Add("Ht", "case.exe");
Console.WriteLine("Value added for key = \"Ht\": {0}", openWith["Ht"]);
}
//遍历哈希表
//元素类型既不是键的类型,也不是值的类型,而是 DictionaryEntry 类型。
Console.WriteLine();
foreach (DictionaryEntry de in openWith)
{
Console.WriteLine("Key = {0}, Value = {1}", de.Key, de.Value);
}
//对哈希表进行排序在这里的定义是对键值对中的key按一定规则重新排列,
//但是实际上这个定义是不能实现的,
//因为我们无法直接在Hashtable中对key进行重新排列,
//如果需要Hashtable提供某种规则的输出,可以采用一种变通的做法:
ArrayList akeys = new ArrayList(openWith.Keys); //别忘了导入System.Collections
akeys.Sort(); //按字母顺序进行排序
foreach(string skey in akeys)
{
Console.Write(skey+ ":");
Console.WriteLine(openWith[skey]);//排序后输出
}
//ICollection 接口是 System.Collections 命名空间中类的基接口。
//public interface IDictionary : ICollection, IEnumerable
//IDictionary 是实现键/值对的集合,如 Hashtable 类。
//IDictionary 接口是键/值对的非通用集合的基接口
//由于IDictionary 的每个元素都是一个键/值对,因此元素类型既不是键的类型,也不是值的类型。而是 DictionaryEntry 类型。
ICollection valueColl = openWith.Values;
Console.WriteLine();
foreach (string s in valueColl)
{
Console.WriteLine("Value = {0}", s);
}
ICollection keyColl = openWith.Keys;
Console.WriteLine();
foreach (string s in keyColl)
{
Console.WriteLine("Key = {0}", s);
}
//移除doc键值对
Console.WriteLine("\nRemove(\"doc\")");
openWith.Remove("doc");
if (!openWith.ContainsKey("doc"))
{
Console.WriteLine("Key \"doc\" is not found.");
}
//移除所有键值对
Console.WriteLine();
openWith.Clear();
Console.WriteLine("\"tif\"={0}", openWith["rtf"]);
Console.WriteLine();
Console.WriteLine("Hashtable 中的键/值对的数目={0}", openWith.Count);
Console.ReadKey();
}
}
}
运行结果如下:
Hashtable 中的键/值对的数目=4
System.Collections.Hashtable
An element with Key = "txt" already exists.
For key = "rtf", value = wordpad.exe.
For key = "rtf", value = winword.exe.
For key = "doc", value = word.exe.
For key = "tif", value = .
Value added for key = "ht": hypertrm.exe
Value added for key = "Ht": case.exe
Key = doc, Value = word.exe
Key = rtf, Value = winword.exe
Key = txt, Value = notepad.exe
Key = ht, Value = hypertrm.exe
Key = Ht, Value = case.exe
Key = dib, Value = paint.exe
Key = bmp, Value = paint.exe
bmp:paint.exe
dib:paint.exe
doc:word.exe
ht:hypertrm.exe
Ht:case.exe
rtf:winword.exe
txt:notepad.exe
Value = word.exe
Value = winword.exe
Value = notepad.exe
Value = hypertrm.exe
Value = case.exe
Value = paint.exe
Value = paint.exe
Key = doc
Key = rtf
Key = txt
Key = ht
Key = Ht
Key = dib
Key = bmp
Remove("doc")
Key "doc" is not found.
"tif"=
Hashtable 中的键/值对的数目=0