List<T>
using System.Collection.Generic;
public class Program {
public static void Main() {
List<int> list = new List<int>(3);
for (int i=0; i<8; i++) {
<span style="white-space:pre"> </span> list.Add(i);
<span style="white-space:pre"> </span> <span style="white-space:pre"> </span> System.Console.WriteLine(“Count: {0} Capacity: {1}”,
<span style="white-space:pre"> </span> list.Count, list.Capacity);
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span> <span style="white-space:pre"> </span>list.TrimExcess();
<span style="white-space:pre"> </span>System.Console.WriteLine(“Count: {0} Capacity: {1}”,
<span style="white-space:pre"> </span>list.Count, list.Capacity );
}
}
HashTable
using System.Collections;
public class Program {
public static void Main() {
//Create a hash table
Hashtable ht = new Hashtable();
//add elements to the table
ht.Add("house", "dwelling");
// ht.Add("house", "houseing"); //HashTable的键值是唯一的,否则会有异常
ht.Add("car", "means of transport");
ht.Add("apple", "Ediable fruit");
ht["tractor"] = "Farm implements";
//首先,使用Keys属性取回键的集合。接着,使用每个键索引ht,得到每个键相关的内容
ICollection c=ht.Keys;
foreach (string str in c)
System.Console.WriteLine(str+":"+ht[str]);
}
}
SortedList
SortedList类创建了一个存储"键/值"对的集合,该集合中的元素参照键的值顺序地存储。
通过枚举器访问集合
using System.Collections;
using System;
using System.Collections.Generic;
public class Program {
public static void Main() {
//Create a hash table
Hashtable ht = new Hashtable();
//add elements to the table
ht.Add("house", "dwelling");
// ht.Add("house", "houseing"); //HashTable的键值是唯一的,否则会有异常
ht.Add("car", "means of transport");
ht.Add("apple", "Ediable fruit");
ht["tractor"] = "Farm implements";
//首先,使用Keys属性取回键的集合。接着,使用每个键索引ht,得到每个键相关的内容
ICollection c=ht.Keys;
foreach (string str in c)
System.Console.WriteLine(str+":"+ht[str]);
IDictionaryEnumerator ide = ht.GetEnumerator();
while (ide.MoveNext()) {
Console.Write(ide.Entry.Key+":"+ide.Entry.Value);
Console.Write(ide.Key + ":" + ide.Value);
Console.WriteLine();
}
List<int> list = new List<int>(5);
list.Add(2);
list.Add(4);
list.Add(7);
list.Add(9);
IEnumerator idic = list.GetEnumerator();
while(idic.MoveNext()){
Console.WriteLine(idic.Current);
}
}
}