Home » C#.Net

Getting an element of Collection<T> from specified index in C#

Here, we are going to learn how to get an element of Collection<T> from the specified index in C#?
Submitted by IncludeHelp, on September 23, 2019

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

To access an element of the Collection<T>, we use Collection<T>.Item[Int32 index] property.

Syntax:

    Collection<T>.Item[Int32 index];

Note: It may return exception (ArgumentOutOfRangeException), if index is either less than 0 or greater than the count.

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.

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



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.