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);
}

  

  

posted @   microsoft-zhcn  阅读(82)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示