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); } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?