c#数组获取元素的索引_获取元素集合 从C#中的指定索引
c#數(shù)組獲取元素的索引
Given a Collection<T> of integer types, and an index, we have to access the element from the given index.
給定一個(gè)整數(shù)類型的Collection <T>和一個(gè)索引,我們必須從給定索引訪問元素。
To access an element of the Collection<T>, we use Collection<T>.Item[Int32 index] property.
要訪問Collection <T>的元素,我們使用Collection <T> .Item [Int32 index]屬性 。
Syntax:
句法:
Collection<T>.Item[Int32 index];Note: It may return exception (ArgumentOutOfRangeException), if index is either less than 0 or greater than the count.
注意:如果index小于0或大于count,則它可能返回異常( ArgumentOutOfRangeException )。
用C#代碼訪問Collection <T>的元素 (C# code to access an element of Collection<T>)
using System; using System.Collections.Generic; using System.Collections.ObjectModel;class IncludeHelp {public static void Main(){// declaring a collection of integersCollection<int> iColl = new Collection<int>();// adding elements to the collectioniColl.Add(100);iColl.Add(200);iColl.Add(300);iColl.Add(400);// displaying total number of elementsConsole.WriteLine("Total number of elements: " + iColl.Count);// displaying elements from given indexConsole.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]);} }Output
輸出量
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: 400Displaying exception
顯示異常
Here, we will access an element from -1 index that will generate "ArgumentOutOfRangeException" exception.
在這里,我們將從-1索引訪問一個(gè)元素,該元素將生成“ ArgumentOutOfRangeException”異常。
using System; using System.Collections.Generic; using System.Collections.ObjectModel;class IncludeHelp {public static void Main(){// declaring a collection of integersCollection<int> iColl = new Collection<int>();// adding elements to the collectioniColl.Add(100);iColl.Add(200);iColl.Add(300);iColl.Add(400);// displaying total number of elementsConsole.WriteLine("Total number of elements: " + iColl.Count);// displaying elements from given indexConsole.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]);// displaying element from index "-1"Console.WriteLine("Element at index " + -1 + " is: " + iColl[-1]);} }Output
輸出量
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: 400Unhandled Exception: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: indexat System.ThrowHelper.ThrowArgumentOutOfRange_IndexException () [0x0000c] in <65984520577646ec9044386ec4a7b3dd>:0at System.Collections.Generic.List`1[T].get_Item (System.Int32 index) [0x00009] in <65984520577646ec9044386ec4a7b3dd>:0at System.Collections.ObjectModel.Collection`1[T].get_Item (System.Int32 index) [0x00000] in <65984520577646ec9044386ec4a7b3dd>:0at IncludeHelp.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: indexat System.ThrowHelper.ThrowArgumentOutOfRange_IndexException () [0x0000c] in <65984520577646ec9044386ec4a7b3dd>:0at System.Collections.Generic.List`1[T].get_Item (System.Int32 index) [0x00009] in <65984520577646ec9044386ec4a7b3dd>:0at System.Collections.ObjectModel.Collection`1[T].get_Item (System.Int32 index) [0x00000] in <65984520577646ec9044386ec4a7b3dd>:0at IncludeHelp.Main () [0x00129] in <775a4ba6f9ff4ee287095185056138d8>:0翻譯自: https://www.includehelp.com/dot-net/getting-an-element-of-collection-t-from-specified-index-in-csharp.aspx
c#數(shù)組獲取元素的索引
總結(jié)
以上是生活随笔為你收集整理的c#数组获取元素的索引_获取元素集合 从C#中的指定索引的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DNF中为什么和有的人不能组队?
- 下一篇: 踩过界剧情介绍