c#中Hashtable用法简述
来源:互联网 发布:大数据软件平台公司 编辑:程序博客网 时间:2024/06/06 00:15
1.使用hashtable前.需要添加System.Collections的引用
using System.Collections;
2.添加元素
Hashtable ht =new Hashtable();
ht.Add(key,value);// key,value可以是任何类型
如果key有重复会掷出运行时异常,你可以这样处理
if(ht.Contains(key)==false){
ht.Add(key,value);// 不存在则添加
}
也可以这样处理,效率要高些
try{
ht.Add(key,value);
}
catch{
// 不处理重复异常
}
3.删除元素
ht.Remove(key);
4.删除所有
ht.Clear();
5.判断键是否已经存在
ht.Contains(key) // 这个刚才已经用过了
6.遍历键
foreach(Object key in ht.Keys){
}
7.遍历值
foreach(Object value in ht.Values){
}
8.同时遍历键值对
foreach(DictionaryEntry de in ht)
{
Console.WriteLine(de.Key);// 取得键
Console.WriteLine(de.Value);// 取得值
}
9.排序输出(对值也是一样处理)
ArrayList akeys = new ArrayList(ht.Keys);
akeys.Sort();
- c#中Hashtable用法简述
- c#中Hashtable用法简述
- c#中Hashtable用法简述
- c#中Hashtable用法简述
- c# 哈希表(Hashtable)简述
- C#中HashTable的用法
- C#中HashTable的用法
- C#中HashTable的用法
- C#中HashTable的用法
- C#中HashTable的用法
- C#中HashTable的用法
- C#中HashTable的用法
- C#中HashTable的用法
- C#中HashTable的用法
- C#中HashTable的用法
- C#中HashTable的用法
- C#中HashTable的用法
- C#中HashTable的用法
- 字符串函数strncat
- 字符串函数strncat
- 字符串函数strncmp
- 字符串函数strnicmp,strncmpi
- IEEE 1588 Sync和Follow_Up格式
- c#中Hashtable用法简述
- 字符串函数strncpy
- 字符串函数strpbrk
- 电子地图
- 字符串函数strrev
- 字符串函数strset
- 字符串函数strstr
- 字符串函数strtok
- 字符串函数strtok