SortedList.GetByIndex(Int32) Method is used to get the value at the specified index of a SortedList object.
Syntax:
public virtual object GetByIndex (int index);
Here index is the zero-based index of the value to get.
Return Value: It returns the value at the specified index of the SortedList object.
Exception: This method will throw ArgumentOutOfRangeException
if the index is outside the range of valid indexes for the SortedList object.
Below programs illustrate the use of above-discussed method:
Example 1:
using System;
using System.Collections;
class Geeks {
public static void Main(String[] args)
{
SortedList mylist = new SortedList();
mylist.Add( "1" , "C++" );
mylist.Add( "2" , "Java" );
mylist.Add( "3" , "DSA" );
mylist.Add( "4" , "Python" );
mylist.Add( "5" , "C#" );
int i = 4;
Console.WriteLine( "Value at index {0} is {1}" ,
i, mylist.GetByIndex(i));
}
}
|
Output:
Value at index 4 is C#
Example 2:
using System;
using System.Collections;
class Geeks {
public static void Main(String[] args)
{
SortedList mylist = new SortedList();
mylist.Add( "First" , "Ram" );
mylist.Add( "Second" , "Shyam" );
mylist.Add( "Third" , "Mohit" );
mylist.Add( "Fourth" , "Rohit" );
mylist.Add( "Fifth" , "Manish" );
int i = 7;
Console.WriteLine( "Value at index {0} is {1}" ,
i, mylist.GetByIndex(i));
}
}
|
Runtime Error:
Unhandled Exception:
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Reference:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
01 Feb, 2019
Like Article
Save Article