C#中从指定索引获取Collection 的元素
给定一个整数类型的Collection<T>和一个索引,我们必须从给定索引访问元素。
要访问Collection<T>的元素,我们使用Collection<T>.Item[Int32index]属性。
语法:
Collection<T>.Item[Int32 index];
注意:如果index小于0或大于count,则可能返回异常(ArgumentOutOfRangeException)。
用C#代码访问Collection<T>的元素
using System; using System.Collections.Generic; using System.Collections.ObjectModel; class nhooo { public static void Main() { //声明整数的集合 Collection<int> iColl = new Collection<int>(); //向集合中添加元素 iColl.Add(100); iColl.Add(200); iColl.Add(300); iColl.Add(400); //显示元素总数 Console.WriteLine("Total number of elements: " + iColl.Count); //显示给定索引中的元素 Console.WriteLine("Element at index " + 0 + " is: " + iColl[0]); Console.WriteLine("Element at index " + 1 + " is: " + iColl[1]); Console.WriteLine("Element at index " + 2 + " is: " + iColl[2]); Console.WriteLine("Element at index " + 3 + " is: " + iColl[3]); } }
输出结果
Total number of elements: 4 Element at index 0 is: 100 Element at index 1 is: 200 Element at index 2 is: 300 Element at index 3 is: 400
显示异常
在这里,我们将从-1索引访问一个元素,该元素将生成“ArgumentOutOfRangeException”异常。
using System; using System.Collections.Generic; using System.Collections.ObjectModel; class nhooo { public static void Main() { //声明整数的集合 Collection<int> iColl = new Collection<int>(); //向集合中添加元素 iColl.Add(100); iColl.Add(200); iColl.Add(300); iColl.Add(400); //显示元素总数 Console.WriteLine("Total number of elements: " + iColl.Count); //显示给定索引中的元素 Console.WriteLine("Element at index " + 0 + " is: " + iColl[0]); Console.WriteLine("Element at index " + 1 + " is: " + iColl[1]); Console.WriteLine("Element at index " + 2 + " is: " + iColl[2]); Console.WriteLine("Element at index " + 3 + " is: " + iColl[3]); //显示来自索引“-1”的元素"-1" Console.WriteLine("Element at index " + -1 + " is: " + iColl[-1]); } }
输出结果
Total number of elements: 4 Element at index 0 is: 100 Element at index 1 is: 200 Element at index 2 is: 300 Element at index 3 is: 400 Unhandled Exception: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index at System.ThrowHelper.ThrowArgumentOutOfRange_IndexException () [0x0000c] in <65984520577646ec9044386ec4a7b3dd>:0 at System.Collections.Generic.List`1[T].get_Item (System.Int32 index) [0x00009] in <65984520577646ec9044386ec4a7b3dd>:0 at System.Collections.ObjectModel.Collection`1[T].get_Item (System.Int32 index) [0x00000] in <65984520577646ec9044386ec4a7b3dd>:0 atnhooo.Main() [0x00129] in <775a4ba6f9ff4ee287095185056138d8>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index at System.ThrowHelper.ThrowArgumentOutOfRange_IndexException () [0x0000c] in <65984520577646ec9044386ec4a7b3dd>:0 at System.Collections.Generic.List`1[T].get_Item (System.Int32 index) [0x00009] in <65984520577646ec9044386ec4a7b3dd>:0 at System.Collections.ObjectModel.Collection`1[T].get_Item (System.Int32 index) [0x00000] in <65984520577646ec9044386ec4a7b3dd>:0 atnhooo.Main() [0x00129] in <775a4ba6f9ff4ee287095185056138d8>:0