System.Collections.Generic.Dictionary; //鍵/值對集合System.Collections.Generic.KeyValuePair; //鍵/值對結構, 作為 Dictionary 的一個元素存在System.Collections.Generic.SortedDictionary; //相當於 Key 能自動排序 DictionarySystem.Collections.Generic.SortedList; //和 SortedDictionary 功能相似, 但內部演算法不同, 其 Keys、Values 可通過索引訪問System.Collections.Generic.HashSet; //無序、無重複的元素集合System.Collections.Generic.SortedSet; //相當於能自動排序的 HashSetSystem.Collections.Generic.List; //相當於泛型的 ArrayList, 元素可重複、可排序、可插入、可索引訪問System.Collections.Generic.Queue; //隊列, 先進先出System.Collections.Generic.Stack; //堆棧, 後進先出System.Collections.Generic.LinkedList; //雙向鏈表System.Collections.Generic.LinkedListNode; //LinkedList 的節點System.Collections.Generic.SynchronizedCollection; //安全執行緒的集合System.Collections.Generic.SynchronizedReadOnlyCollection; //安全執行緒的唯讀集合System.Collections.Generic.SynchronizedKeyedCollection; //安全執行緒的鍵/值集合
Dictionary、KeyValuePair:
protected void Button1_Click(object sender, EventArgs e){ Dictionary<string int> dict = new Dictionary<string int>(); dict.Add("K1", 123); dict["K2"] = 456; dict.Add("K3", 789); string str = ""; foreach (KeyValuePair<string int> k in dict) { str += string.Format("{0}-{1}; ", k.Key, k.Value); //K1-123; K2-456; K3-789; } TextBox1.Text = str;}</string></string></string>
SortedDictionary:
protected void Button1_Click(object sender, EventArgs e){ SortedDictionary<string int> dict = new SortedDictionary<string int>(); dict.Add("K3", 333); dict["K1"] = 111; dict.Add("K2", 222); SortedDictionary<string int>.KeyCollection ks = dict.Keys; SortedDictionary<string int>.ValueCollection vs = dict.Values; string s1, s2, s3; s1 = s2 = s3 = ""; foreach (KeyValuePair<string int> k in dict) { s1 += string.Format("{0}-{1}; ", k.Key, k.Value); //K1-111; K2-222; K3-333; } foreach (string s in ks) { s2 += s + "; "; } //K1; K2; K3; foreach (int n in vs) { s3 += n.ToString() + "; "; } //111; 222; 333; TextBox1.Text = s1 + "\n" + s2 + "\n" + s3;}</string></string></string></string></string>
SortedList:
protected void Button1_Click(object sender, EventArgs e){ SortedList<string int> dict = new SortedList<string int>(); dict.Add("K3", 333); dict["K1"] = 111; dict.Add("K2", 222); string s1, s2, s3; s1 = s2 = s3 = ""; foreach (KeyValuePair<string int> k in dict) { s1 += string.Format("{0}-{1}; ", k.Key, k.Value); //K1-111; K2-222; K3-333; } s2 = dict.Keys[0]; //K1 s3 = dict.Values[0].ToString(); //111 TextBox1.Text = s1 + "\n" + s2 + "\n" + s3;}</string></string></string>
HashSet、SortedSet:
protected void Button1_Click(object sender, EventArgs e){ HashSet<string> hs = new HashSet<string>(); hs.Add("ccc"); hs.Add("bbb"); hs.Add("aaa"); SortedSet<string> ss = new SortedSet<string>(); ss.Add("ccc"); ss.Add("bbb"); ss.Add("aaa"); string s1 = "", s2 = ""; foreach (string s in hs) { s1 += s + " "; } //ccc bbb aaa foreach (string s in ss) { s2 += s + " "; } //aaa bbb ccc TextBox1.Text = s1 + "\n" + s2;}</string></string></string></string>
List:
protected void Button1_Click(object sender, EventArgs e){ List<int> list = new List<int>(); list.Add(11); list.Add(22); list.Insert(0, 33); string s1, s2 = "", s3, s4 = ""; s1 = list[0].ToString(); //33 for (int i = 0; i </int></int>
LinkedList、LinkedListNode:
protected void Button1_Click(object sender, EventArgs e){ LinkedList<string> list = new LinkedList<string>(); list.AddFirst("aaa"); list.AddLast("bbb"); list.AddFirst("ccc"); list.AddAfter(list.First, "ddd"); list.AddBefore(list.Last, "eee"); string s1 = "", s2 = "", s3 = "", s4 = "", s5 = ""; foreach (string s in list) { s1 += s + " "; } //ccc ddd aaa eee bbb LinkedListNode<string> node = list.First; s2 = node.Value.ToString(); //ccc node = node.Next; s3 = node.Value.ToString(); //ddd node = list.Last.Previous.Previous; s4 = node.Value.ToString(); //aaa list.Remove("eee"); list.RemoveFirst(); list.RemoveLast(); node = list.First; while (node != null) { s5 += node.Value.ToString() + " "; //ddd aaa node = node.Next; } TextBox1.Text = s1 + "\n" + s2 + "\n" + s3 + "\n" + s4 + "\n" + s5;}</string></string></string>