下面介绍c#的集合类
<强> 1 arraylist 强>
使用系统; 使用System.Collections.Generic; 使用来; 使用text; 使用System.Threading.Tasks; 使用System.Collections; 名称空间动态数组ArrayList { 类项目 { 静态void Main (string [] args) { ArrayList a1=new ArrayList (); a1.Add (100); foreach (int数在新int [6] {9、3、7、2、4、8}) { a1.Add(数量); } int[]科学=new int [2] {11 12}; a1.AddRange(科学); a1.Remove (3); a1.RemoveAt (3); ArrayList al2=new ArrayList (a1.GetRange (1、3)); Console.WriteLine(“遍历方法1:”); 我在a1 foreach (int) { Console.WriteLine(我); } Console.WriteLine(“遍历方法2:”); for (int i=0;我& lt;al2.Count;我+ +) { Console.WriteLine (al2[我]); } Console.ReadLine (); } } }
<强> 2,堆栈强>
使用系统; 使用System.Collections.Generic; 使用来; 使用text; 使用System.Threading.Tasks; 使用System.Collections; 名称空间堆栈集合类 { 类项目 { 静态void Main (string [] args) { 堆栈栈s1=new (); 堆栈栈s2=new (); foreach (int我在新int [4] {1, 2, 3, 4}) { s1.Push(我); s2.Push(我); } 我在s1 foreach (int) { Console.WriteLine(我); } s1.Pop (); Console.WriteLine(出栈”); 我在s1 foreach (int) { Console.WriteLine(我); } int num=(int) s2.Peek (); Console.WriteLine(“弹出最后一项{0}”,num); 我在s2 foreach (int) { Console.WriteLine(我); } Console.ReadLine (); } } }队列
<强> 3 强>
使用系统; 使用System.Collections.Generic; 使用来; 使用text; 使用System.Threading.Tasks; 使用System.Collections; 名称空间队列集合类 { 类项目 { 静态void Main (string [] args) { 队列q1=new队列(); 队列q2=new队列(); foreach (int我在新int [4] {1, 2, 3, 4}) { q1.Enqueue(我); q2.Enqueue(我); } 我在第一季度foreach (int) { Console.WriteLine(我); } q1.Dequeue (); Console.WriteLine (“q1出队”); 我在第一季度foreach (int) { Console.WriteLine(我); } int num=(int) q2.Peek (); Console.WriteLine(“取q2开始处{0}”,num); 我在q2 foreach (int) { Console.WriteLine(我); } Console.ReadLine (); } } }
<强> 4 hashtable 强>
使用系统; 使用System.Collections.Generic; 使用来; 使用text; 使用System.Threading.Tasks; 使用System.Collections; 名称空间散列表集合类 { 类项目 { 静态void Main (string [] args) { Hashtable h=new散列表(); h.Add (“E”,“E”); h。添加(“B”、“B”); h。添加(“C”、“C”); h。添加(“A”,“A”); foreach (DictionaryEntry e h) { 控制台。写({0},{1},e。键,e.Value); } Console.WriteLine (); 字符串s=(字符串)h [C]; Console.WriteLine(年代); 如果(h.Contains (E)) { Console.WriteLine(含有E”); } Console.WriteLine (h (A)); h.Remove (h (A)); h.Clear (); foreach (DictionaryEntry e h) { 控制台。写({0},{1},e。键,e.Value); } Console.ReadLine (); } } }
<强> 5 sortedlist 强>
使用系统; 使用System.Collections.Generic; 使用来; 使用text; 使用System.Threading.Tasks; 使用System.Collections; 名称空间SortedList集合类 { 类项目 { 静态void Main (string [] args) { SortedList s1=new SortedList (); s1 [c]=41; s1 [a]=42; s1 [d]=11; s1 [b]=13; foreach (s1 DictionaryEntry e) { 字符串s=(字符串)e.Key; int i=(int) e.Value; 控制台。写({0},{1},s, i); } Console.ReadLine (); } } }
以上所述是小编给大家介绍的c#集合类用法实例代码详解,希望对大家有所帮助,如果大家有任何疑问请给我留的言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!