c#数组获取元素的索引_获取元素集合 从C#中的指定索引

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 16:03   2051   0

c#数组获取元素的索引

Given a Collection<T> of integer types, and an index, we have to access the element from the given index.

给定一个整数类型的Collection <T>和一个索引,我们必须从给定索引访问元素。

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 integers
        Collection<int> iColl = new Collection<int>();

        // adding elements to the collection
        iColl.Add(100);
        iColl.Add(200);
        iColl.Add(300);
        iColl.Add(400);

        // displaying total number of elements
        Console.WriteLine("Total number of elements: " + iColl.Count);

        // displaying elements from given index
        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]);

    }
}

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: 400

Displaying exception

显示异常

Here, we will access an element from -1 index that will generate "ArgumentOutOfRangeException" exception.

在这里,我们将从-1索引访问一个元素,该元素将生成“ ArgumentOutOfRangeException”异常。

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

class IncludeHelp
{
    public static void Main()
    {
        // declaring a collection of integers
        Collection<int> iColl = new Collection<int>();

        // adding elements to the collection
        iColl.Add(100);
        iColl.Add(200);
        iColl.Add(300);
        iColl.Add(400);

        // displaying total number of elements
        Console.WriteLine("Total number of elements: " + iColl.Count);

        // displaying elements from given index
        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]);
        
        // 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: 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
  at 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: 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
  at 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#数组获取元素的索引

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP