原帖地址:http://www.cnblogs.com/gisera/archive/2009/02/28/1400500.html 若有问题,请通知我,我立即删除
Hashtable 用法
在 .NET Framework 中, Hashtable 是 System.Collections 命名空间提供的一个容器,用于处理和表现类似 key/value 的键值对,其中 key 通常可用来快速查找,同时 key 是区分大小写; value 用于存储对应于 key 的值。 Hashtable 中 key/value 键值对均为 object 类型,所以 Hashtable 可以支持任何类型的 key/value 键值对 . 。
在哈希表中添加一个 key/value 键值对: HashtableObject.Add(key,value);
在哈希表中去除某个 key/value 键值对: HashtableObject.Remove(key);
从哈希表中移除所有元素: HashtableObject.Clear();
判断哈希表是否包含特定键 key : HashtableObject.Contains(key);
Hashtable ht = new Hashtable ();
ht .Add ("a" , 123);
ht .Add ("b" , 456);
// 遍历哈希表需要用到DictionaryEntry Object
foreach (DictionaryEntry de in ht )
{
MessageBox .Show (de .Key .ToString () + " " + de .Value .ToString ());
}
// 对哈希表进行排序
ArrayList akeys = new ArrayList (ht .Keys ); // 别忘了导入System.Collections
akeys .Sort (); // 按字母顺序进行排序
foreach (string skey in akeys )
{
MessageBox .Show (skey + ":" );
MessageBox .Show (ht [skey ].ToString ());// 排序后输出
}
ArrayList 用法
private static void AddToList (ArrayList list , string p )
{
if (list .Contains (p ) == false )
list .Add (p );
}
private void button1_Click (object sender , EventArgs e )
{
ArrayList list = new ArrayList ();
AddToList (list , "Table1" );
AddToList (list , "Table4" );
AddToList (list , "Table1" );
AddToList (list , "Table3" );
AddToList (list , "Table2" );
AddToList (list , "Table2" );
foreach (string s in list )
{
MessageBox .Show (s );
}
}
List
List <string > listStr = new List <string >();
listStr .Add ("123" );
listStr .Add ("456" );
listStr .Add ("789" );
MessageBox .Show (listStr [2]);// ” 789 ”
Dictionary
泛型的优点( C# 编程指南)
C# 中典型的范型结构除了熟悉的 IList , HashTable 之外还有一个并不常见的Dictionary 集合。
相比较而言,Dictionary 的性能是最好的,也属于轻便型的集合。效率要大于HashTable ,其主要原因是Dictionary 支持强类型声明的。
在公共语言运行库和 C# 语言的早期版本中,通用化是通过在类型与通用基类型 Object 之间进行强制转换来实现的,泛型提供了针对这种限制的解决方案。通过创建泛型类,您可以创建一个在编译时类型安全的集合。
添加到 ArrayList 中的任何引用或值类型都将隐式地向上强制转换为 Object 。如果项是值类型,则必须在将其添加到列表中时进行装箱操作,在检索时进行取消装箱操作。强制转换以及装箱和取消装箱操作都会降低性能;在必须对大型集合进行循环访问的情况下,装箱和取消装箱的影响非常明显。
对于客户端代码,与 ArrayList 相比,使用 List<T> 时添加的唯一语法是声明和实例化中的类型参数。虽然这稍微增加了些编码的复杂性,但好处是您可以创建一个比 ArrayList 更安全并且速度更快的列表,特别适用于列表项是值类型的情况。
Dictionary 泛型类提供了从一组键到一组值的映射。字典中的每个添加项都由一个值及其相关联的键组成。通过键来检索值的速度是非常快的,接近于 O(1) ,这是因为Dictionary 类是作为一个哈希表来实现的。
1 、
Dictionary <int , string > fruit = new Dictionary <int , string >();
// 加入重复键会引发异常
fruit .Add (1, " 苹果" );
fruit .Add (2, " 桔子" );
fruit .Add (3, " 香蕉" );
fruit .Add (4, " 菠萝" );
// 因为引入了泛型,所以键取出后不需要进行Object 到int 的转换,值的集合也一样
foreach (int i in fruit .Keys )
{
MessageBox .Show (" 键是:" +i .ToString ()+ " 值是:" + fruit [i ]);
}
2 、
Dictionary <string , string > fruit = new Dictionary <string , string >();
// 加入重复键会引发异常
fruit .Add ("1" , " 苹果" );
fruit .Add ("2" , " 桔子" );
fruit .Add ("3" , " 香蕉" );
fruit .Add ("4" , " 菠萝" );
// 因为引入了泛型,所以键取出后不需要进行Object 到int 的转换,值的集合也一样
foreach (string i in fruit .Keys )
{
MessageBox .Show (" 键是:" +i .ToString ()+ " 值是:" + fruit [i ]);
}
=====================================================================
Strong typed ArrayList -> List