Hashtable
Dictionary与Hashtable都是.Net Framework中的字典类,能够根据键快速查找值
Dictionary
- 有泛型优势(类型安全,性能更好),对于值类型,不存在装箱和拆箱的性能损耗
- 读取速度快(体现在单条数据上)
- 容量利用更充分
- 有序(遍历时输出的顺序就是加入的顺序)
Hashtable
- 适合多线程
- 通过静态方法Synchronize方法可获得完全线程安全的类型
- 无序 ```csharp using System.Collections; //使用Hashtable时,必须引入这个命名空间
//创建一个Hashtable实例 Hashtable ht=new Hashtable();
//添加keyvalue键值对 ht.Add(“E”,”e”); ht.Add(“A”,”a”);
//移除 ht.Remove(“C”); ht.Clear();//移除所有元素
//获取 Console.WriteLine(ht[“A”]);
//判断哈希表是否包含特定键,其返回值为true或false if(ht.Contains(“E”))
//遍历方法一:遍历哈希表中的键 foreach (string key in ht.Keys) { Console.WriteLine(ht[key]); }
//遍历方法二:遍历哈希表中的值 foreach(string value in ht.Values) { Console.WriteLine(value); }
//遍历方法三:遍历哈希表中的键值 foreach (DictionaryEntry de in ht) { Console.WriteLine(de.key); Console.WriteLine(de.Value); }
<a name="UoSDz"></a>## List<T>**List<数据类型> 变量名=new list<数据类型>();**<br />List<string> listNew=new List<string>();```csharp//声明List<string> mList = new List<string>();//用集合声明string[] temArr = { "Ha", "Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", "Locu" };List<string> mList = new List<string>(temArr);//添加mList.Add("John");//添加一组string[] temArr = { "Ha","Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", "Locu" };mList.AddRange(temArr);//指定位置添加 Insert(int index, T item);mList.Insert(1, "Hei");//遍历foreach (string s in mList){Console.WriteLine(s);}//删除mList.Remove("Hunter");//下标删除 List. RemoveAt(int index);mList.RemoveAt(0);//从下标index开始,删除count个元素 List. RemoveRange(int index, int count);mList.RemoveRange(3, 2);//判断 List. Contains(T item) 返回值为:true/falsemList.Contains("Hunter")//排序 List. Sort () 默认是元素第一个字母按升序mList.Sort();//自定义排序...//清空mList.Clear();//获取长度int count = mList.Count();//清空mList.Clear();
我啊是啊
