(4)Hashtable类
哈希表,名-值对。类似于字典(比数组更强大)。哈希表是经过优化的,访问下标的对象先散列过。如果以任意类型键值访问其中元素会快于其他集合。GetHashCode()方法返回一个int型数据,使用这个键的值生成该int型数据。哈希表获取这个值最后返回一个索引,表示带有给定散列的数据项在字典中存储的位置。
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
public static void Main()
{
// Creates and initializes a new Hashtable.
Hashtable myHT = new Hashtable();
myHT.Add("one", "The");
myHT.Add("two", "quick");
myHT.Add("three", "brown");
myHT.Add("four", "fox");
// Displays the Hashtable.//清清月儿 http://blog.csdn.net/21aspnet/
Console.WriteLine("The Hashtable contains the following:");
PrintKeysAndValues(myHT);
}
public static void PrintKeysAndValues(Hashtable myHT)
{
foreach (string s in myHT.Keys)
Console.WriteLine(s);
Console.WriteLine(" -KEY- -VALUE-");
foreach (DictionaryEntry de in myHT)
Console.WriteLine(" {0}: {1}", de.Key, de.Value);
Console.WriteLine();
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
public static void Main()
{
// Creates and initializes a new Hashtable.
Hashtable myHT = new Hashtable();
myHT.Add("one", "The");
myHT.Add("two", "quick");
myHT.Add("three", "brown");
myHT.Add("four", "fox");
// Displays the Hashtable.//清清月儿 http://blog.csdn.net/21aspnet/
Console.WriteLine("The Hashtable contains the following:");
PrintKeysAndValues(myHT);
}
public static void PrintKeysAndValues(Hashtable myHT)
{
foreach (string s in myHT.Keys)
Console.WriteLine(s);
Console.WriteLine(" -KEY- -VALUE-");
foreach (DictionaryEntry de in myHT)
Console.WriteLine(" {0}: {1}", de.Key, de.Value);
Console.WriteLine();
}
}
}