1.数组
每个实例化的数组都继承于Array抽象类,Array抽象类实现了IEnumerable,ICollection,IList接口。其中不是每一个方法都实现了,没有实现的抛出异常。
2.List<T>,ArrayList
public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
注意删除元素的时候,先看IEquatable接口,在看重写的Object的equals方法,再看引用。
AsReadOnly()可以返回只读集合,返回ReadOnlyCollection<T>与List<T>相同,只是有些写的方法抛出异常。
public class ArrayList : IList, ICollection, IEnumerable, ICloneable
3.Queue,Queue<T>
public class Queue : ICollection, IEnumerable, ICloneable
public class Queue<T> : IEnumerable<T>, ICollection, IEnumerable
4.Stack,Stack<T>
public class Stack : ICollection, IEnumerable, ICloneable
public class Stack<T> : IEnumerable<T>, ICollection, IEnumerable
5.LinkedList<T>
public class LinkedList<T> : ICollection<T>, IEnumerable<T>, ICollection, IEnumerable, ISerializable, IDeserializationCallback
这是一个双向链表
6.SortedList<T>,SortedList
public class SortedList<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable
这个集合在加入的时候就是排好序的。
<textarea cols="50" rows="15" name="code" class="c-sharp">class Program { static void Main(string[] args) { //和Dictionary操作很像。只是里面的元素是有序的。所以我们要保证key字段能够被排序。它的一个构造方法是接受一个比较器对象,用来对key排序 SortedList<string, int> sortedList = new SortedList<string, int>(); sortedList.Add("xuxu", 1); sortedList.Add("bubu", 2); foreach (KeyValuePair<string,int> item in sortedList) { Console.WriteLine(item.Key + " " + item.Value); } SortedList<A, int> sortedList1 = new SortedList<A, int>(); sortedList1.Add(new A() { Val = 1 }, 1); sortedList1.Add(new A() { Val = 2 }, 2); foreach (KeyValuePair<A, int> item in sortedList1) { Console.WriteLine(item.Key.Val + " " + item.Value); } } } class A : IComparable<A>{ public int Val { get; set; } public int CompareTo(A other) { if (other.Val > this.Val) return -1; else if (other.Val == this.Val) return 0; else return 1; } }</textarea>
7.Dictionary
public class Dictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback
其中有一个构造函数,允许作为key的类型不实现equals和gethashcode,方法是构造函数传入一个IEqualityComparer<T>
TryGetValue(key,out value);如果有则返回true,没有则返回fasle。value取得值。
8.HashTable
查看与Dictionary的区别: http://blog.163.com/jeson_lwj/blog/static/13576108320101187546107/
1:多线程程序中推荐使用 Hashtable, 默认的 Hashtable 允许单线程写入, 多线程读取, 对 Hashtable 进一步调用 Synchronized() 方法可以获得完全线程安全的类型. 而 Dictionary 非线程安全, 必须人为使用 lock 语句进行保护, 效率大减.
2:单线程程序中推荐使用 Dictionary, 有泛型优势, 且读取速度较快, 容量利用更充分.
3:Dictionary 有按插入顺序排列数据的特性 (注: 但当调用 Remove() 删除过节点后顺序被打乱), 因此在需要体现顺序的情境中使用 Dictionary 能获得一定方便.
Hashtable类和 Dictionary<TKey, TValue> 泛型类实现 IDictionary 接口
Dictionary<TKey, TValue> 泛型类还实现 IDictionary<TKey, TValue>泛型接口。因此,这些集合中的每个元素都是一个键/值对。
Dictionary<TKey, TValue>类与 Hashtable 类的功能相同
对于值类型,特定类型(不包括 Object)的 Dictionary<TKey, TValue>的性能优于 Hashtable,这是因为 Hashtable 的元素属于 Object 类型,所以在存储或检索值类型时通常发生装箱和取消装箱操作。
<textarea cols="50" rows="15" name="code" class="c-sharp">Hashtableht=new Hashtable();//实现 IDictionary接口 ht.Add(1,"A"); ht.Add(2,"B"); ht.Add(3,"c"); foreach(DictionaryEntry de in ht)//Hashtable返回的是DictionaryEntry 类型 { de.Key; de.Value; } Dictionary<int,string> myDictionary=new Dictionary<int,string>();//实现IDictionary接口,IDictionary<T key,T value>类 myDictionary.Add(1,"a"); myDictionary.Add(2,"b"); myDictionary.Add(3,"c"); foreach(int i in myDictionary.Keys) { Console.WriteLine("Key="+i+"Value="+myDictionary); } Or foreach(KeyValuePair<string, double> temp in myDictionary)//返回的是KeyValuePair<string, double>泛型数组 { temp.Key; temp.Value; }</textarea>
泛型最常见的用途是泛型集合,命名空间System.Collections.Generic 中包含了一些基于泛型的集合类,使用泛型集合类可以提供更高的类型安全性,还有更高的性能,避免了非泛型集合的重复的装箱和拆箱。 很多非泛型集合类都有对应的泛型集合类,下面是常用的非泛型集合类以及对应的泛型集合类:
ArrayList List<T>
HashTable DIctionary<T>
Queue Queue<T>
Stack Stack<T>
SortedList SortedList<T>
我们用的比较多的非泛型集合类主要有 ArrayList类 和 HashTable类。我们经常用HashTable 来存储将要写入到数据库或者返回的信息,在这之间要不断的进行类型的转化,增加了系统装箱和拆箱的负担,如果我们操纵的数据类型相对确定的化 用 Dictionary<TKey,TValue> 集合类来存储数据就方便多了,例如我们需要在电子商务网站中存储用户的购物车信息( 商品名,对应的商品个数)时,完全可以用 Dictionary<string, int> 来存储购物车信息,而不需要任何的类型转化。
9.ListDictionary
和Hashtable类似,但是是用链表实现的。可以按顺序输出。当元素个数小于10个时,采用它比较快,否则Hashtable比较快
10.HybridDictionary
动态判断,当元素个数比较小的时候采用ListDictionary,当元素个数增大时改用Hashtable.事先不知道个数可以使用它
11.NameValueCollection
类似于有序的字符串值和字符串键的集合,它并没实现IDictionary接口。
string[] AllKeys 属性:所有键的字符串数组。
bool HasKeys ():集合对象中是否包含"键/值"对。
string Get (int index/string key):根据索引号或键名来取得元素的值。
string GetKey (int index):根据索引号取得键名。
<textarea cols="50" rows="15" name="code" class="c-sharp">NameValueCollection nvc = new NameValueCollection(); nvc.Add("a", "aaa"); nvc.Add("b","bbb"); nvc.Add("c","ccc"); foreach (string s in nvc.AllKeys) { Console.WriteLine(nvc[s].ToString()); }</textarea>
12.SortedDictionary<T,V>
与SortedList类似,但是SortedList实现为一个基于数组的链表,但是它实现为字典。SortedList占用内存少,SortedDictionary的元素插入和删除比较快,在用已排好序的数据填充集合时,若不需要修改容量,SortedList<T,V>速度比较快
13.HashSet<T>
public class HashSet<T> : ICollection<T>, IEnumerable<T>, IEnumerable, ISerializable, IDeserializationCallback
其中包含的方法中有一些集合操作。
14.LookUp<T,V>
类似于Dictionary<T,V>,不同的是值是一个V类型的集合,而不是一个V类型的值。不能主动创建,必须调用IEnumerable<T>的ToLookUp方法来获得。
<textarea cols="50" rows="15" name="code" class="c-sharp"> class Program { static void Main(string[] args) { LookupExample(); } public static void LookupExample() { // Create a list of Packages to put into a Lookup data structure. List<Package> packages = new List<Package> { new Package { Company = "Coho Vineyard", Weight = 25.2, TrackingNumber = 89453312L }, new Package { Company = "Lucerne Publishing", Weight = 18.7, TrackingNumber = 89112755L }, new Package { Company = "Wingtip Toys", Weight = 6.0, TrackingNumber = 299456122L }, new Package { Company = "Contoso Pharmaceuticals", Weight = 9.3, TrackingNumber = 670053128L }, new Package { Company = "Wide World Importers", Weight = 33.8, TrackingNumber = 4665518773L } }; // Create a Lookup to organize the packages. Use the first character of Company as the key value. // Select Company appended to TrackingNumber for each element value in the Lookup. Lookup<char, string> lookup = (Lookup<char, string>)packages.ToLookup(p => Convert.ToChar(p.Company.Substring(0, 1)), p => p.Company + " " + p.TrackingNumber); // Iterate through each IGrouping in the Lookup and output the contents. foreach (IGrouping<char, string> packageGroup in lookup) { // Print the key value of the IGrouping. Console.WriteLine(packageGroup.Key); // Iterate through each value in the IGrouping and print its value. foreach (string str in packageGroup) Console.WriteLine(" {0}", str); } // This code produces the following output: // // C // Coho Vineyard 89453312 // Contoso Pharmaceuticals 670053128 // L // Lucerne Publishing 89112755 // W // Wingtip Toys 299456122 // Wide World Importers 4665518773 // Get the number of key-collection pairs in the Lookup. int count = lookup.Count; // Select a collection of Packages by indexing directly into the Lookup. IEnumerable<string> cgroup = lookup['C']; // Output the results. Console.WriteLine("/nPackages that have a key of 'C':"); foreach (string str in cgroup) Console.WriteLine(str); // This code produces the following output: // // Packages that have a key of 'C' // Coho Vineyard 89453312 // Contoso Pharmaceuticals 670053128 // Determine if there is a key with the value 'G' in the Lookup. bool hasG = lookup.Contains('G'); } } class Package { public string Company; public double Weight; public long TrackingNumber; }</textarea>
除了Hashtable都是线程不安全的。位于System.Collections命名空间下的集合,如Hashtable,ArrayList,Stack,Queue等.其均提供了线程同步的一个实现。关于线程安全,请看
http://archive.cnblogs.com/a/1897534/
http://www.cnblogs.com/Mainz/archive/2008/04/06/CSharp_HashTable_Dictionary_ArrayList_Threadsafe.html