Getting an element of Collection from specified index in C#

Here, we will figure out how to get a component of Collection from the predetermined record in C#?

Given a Collection of integer types, and a list, we need to get to the component from the given file.

To get to a component of the Collection, we use Collection.Item[Int32 index] property.

Syntax:

    Collection<T>.Item[Int32 index];

Note: It might return special case (ArgumentOutOfRangeException) if the list is either under 0 or more prominent than the check.

C# code to get to a component of Collection

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

class JustTechReview
{
    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

Showing an Exception

Here, we will get to a component from – 1 file that will produce “ArgumentOutOfRangeException” special case.

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

class JustTechReview
{
    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 JustTechReview.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 JustTechReview.Main () [0x00129] in <775a4ba6f9ff4ee287095185056138d8>:0

Leave a Comment

error: Alert: Content is protected!!