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); }

  1. <a name="UoSDz"></a>
  2. ## List<T>
  3. **List<数据类型> 变量名=new list<数据类型>();**<br />List<string> listNew=new List<string>();
  4. ```csharp
  5. //声明
  6. List<string> mList = new List<string>();
  7. //用集合声明
  8. string[] temArr = { "Ha", "Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", "Locu" };
  9. List<string> mList = new List<string>(temArr);
  10. //添加
  11. mList.Add("John");
  12. //添加一组
  13. string[] temArr = { "Ha","Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", "Locu" };
  14. mList.AddRange(temArr);
  15. //指定位置添加 Insert(int index, T item);
  16. mList.Insert(1, "Hei");
  17. //遍历
  18. foreach (string s in mList)
  19. {
  20. Console.WriteLine(s);
  21. }
  22. //删除
  23. mList.Remove("Hunter");
  24. //下标删除 List. RemoveAt(int index);
  25. mList.RemoveAt(0);
  26. //从下标index开始,删除count个元素 List. RemoveRange(int index, int count);
  27. mList.RemoveRange(3, 2);
  28. //判断 List. Contains(T item) 返回值为:true/false
  29. mList.Contains("Hunter")
  30. //排序 List. Sort () 默认是元素第一个字母按升序
  31. mList.Sort();
  32. //自定义排序
  33. ...
  34. //清空
  35. mList.Clear();
  36. //获取长度
  37. int count = mList.Count();
  38. //清空
  39. mList.Clear();

我啊是啊