演示System.Collections.Generic的各容器类的用法.
包括:Dictionary,KeyValuePair,SortedDic tionary,SortedList,HashSet,SortedSet,List,Queue,Stack等
1 System.Collections.Generic.Dictionary<>; //键/值对集合 2 System.Collections.Generic.KeyValuePair<>; //键/值对结构, 作为 Dictionary<> 的一个元素存在 3 System.Collections.Generic.SortedDictionary<>; //相当于 Key 能自动排序 Dictionary<> 4 System.Collections.Generic.SortedList<>; //和 SortedDictionary<> 功能相似, 但内部算法不同, 其 Keys、Values 可通过索引访问 5 6 System.Collections.Generic.HashSet<>; //无序、无重复的元素集合 7 System.Collections.Generic.SortedSet<>; //相当于能自动排序的 HashSet<> 8 System.Collections.Generic.List<>; //相当于泛型的 ArrayList, 元素可重复、可排序、可插入、可索引访问 9 10 System.Collections.Generic.Queue<>; //队列, 先进先出 11 System.Collections.Generic.Stack<>; //堆栈, 后进先出 12 13 System.Collections.Generic.LinkedList<>; //双向链表 14 System.Collections.Generic.LinkedListNode<>; //LinkedList<> 的节点 15 16 System.Collections.Generic.SynchronizedCollection<>; //线程安全的集合 17 System.Collections.Generic.SynchronizedReadOnlyCollection<>; //线程安全的只读集合 18 System.Collections.Generic.SynchronizedKeyedCollection<>; //线程安全的键/值集合 19 20 Dictionary<>、KeyValuePair<>: 21 protected void Button1_Click(object sender, EventArgs e) 22 { 23 Dictionary<string, int> dict = new Dictionary<string, int>(); 24 dict.Add("K1", 123); 25 dict["K2"] = 456; 26 dict.Add("K3", 789); 27 28 string str = ""; 29 foreach (KeyValuePair<string, int> k in dict) 30 { 31 str += string.Format(