C# 集合
C# 集合
using System.Data;
using System.Collections;
//数组(Array)
string[] arr = { "Hello", "World" };
int[] nums = { 1, 99, 2, 66, 15, 8 };
//哈希表(Hashtable)
Hashtable hashtable = new Hashtable();
hashtable.Add("H", "Hello");
hashtable.Add("W", "World");
//动态数组(ArrayList)
ArrayList arrayList = new ArrayList();
arrayList.Add("Hello");
arrayList.Add("World");
//字典(Dictionary)
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("H", "Hello");
dic.Add("W", "World");
//排序列表(SortedList)
SortedList sortedList = new SortedList();
sortedList.Add("A", "Hello");
sortedList.Add("B", "World");
//泛型(Generic)
List<string> list = new List<string>();
list.Add("Hello");
list.Add("World");
foreach (string c in list)
{
Console.Write(c + " ");
}
//堆栈(Stack),后进先出的对象集合
Stack stack = new Stack();
stack.Push("Hello");
stack.Push("World");
foreach (string c in stack)
{
Console.Write(c + " ");
}
//队列(Queue),先进先出的对象集合
Queue queue = new Queue();
queue.Enqueue("Hello");
queue.Enqueue("World");
foreach (string c in queue)
{
Console.Write(c + " ");
}
//表格(DataTable)
DataTable datatable = new DataTable();
datatable.Columns.Add("A");
datatable.Columns.Add("B");
datatable.Rows.Add(new Object[] { "H", "Hello" });
datatable.Rows.Add(new Object[] { "W", "World" });
IEnumerable<int> items = from x in nums where x > 10 select x;
foreach (int item in items)
{
Console.WriteLine(item);
}
var dt = from a in datatable.AsEnumerable()
select new
{
A = a.Field<string>("A"),
B = a.Field<string>("B")
};
foreach (var item in dt)
{
Console.WriteLine(item.B);
}

浙公网安备 33010602011771号