C# | Getting the key at the specified index of a SortedList object Last Updated : 01 Feb, 2019 Comments Improve Suggest changes Like Article Like Report SortedList.GetKey(Int32) Method is used to get the key at the specified index of a SortedList object. Syntax: public virtual object GetKey (int index); Here, index is the zero-based index of the key to get. Return Value: This method returns the key at the specified index of the SortedList object. Exception: This method throws 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: CSharp // C# code to get the key at // the specified index of a // SortedList object using System; using System.Collections; class Geeks { // Main Method public static void Main(String[] args) { // Creating a SortedList of integers SortedList mylist = new SortedList(); // Adding elements to SortedList mylist.Add("key1", "C++"); mylist.Add("key2", "Java"); mylist.Add("key3", "DSA"); mylist.Add("key4", "Python"); mylist.Add("key5", "C#"); // storing the value of // index that needed by // used into a variable int i = 2; // getting the key at index 2 Console.WriteLine("Key at index {0} is {1}", i, mylist.GetKey(i)); } } Output: Key at index 2 is key3 Example 2: CSharp // C# code to get the key at // the specified index of a // SortedList object using System; using System.Collections; class Geeks { // Main Method public static void Main(String[] args) { // Creating a SortedList of integers SortedList mylist = new SortedList(); // Adding elements to SortedList mylist.Add("First", "Ram"); mylist.Add("Second", "Shyam"); mylist.Add("Third", "Mohit"); mylist.Add("Fourth", "Rohit"); mylist.Add("Fifth", "Manish"); // storing the value of // index that needed by // used into a variable // it will throw an exception // as index is out of range of // valid indexes of SortedList object int i = 7; // getting the key at index 7 Console.WriteLine("Key at index {0} is {1}", i, mylist.GetKey(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: https://docs.microsoft.com/en-us/dotnet/api/system.collections.sortedlist.getkey?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Getting the key at the specified index of a SortedList object K Kirti_Mangal Follow Improve Article Tags : C# CSharp-method CSharp-Collections-Namespace CSharp-Collections-SortedList Similar Reads C# | Getting the index of the specified key in a SortedList object SortedList.IndexOfKey(Object) Method is used to get the zero-based index of the specified key in a SortedList object. Syntax: public virtual int IndexOfKey (object key); Here, key is the Key which is to be located in the SortedList object. Return Value: This method returns the zero-based index of ty 3 min read C# | Getting the value at the specified index of a SortedList object 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 2 min read C# | Getting index of the specified value in a SortedList object SortedList.IndexOfValue(Object) Method is used to get the zero-based index of the first occurrence of the specified value in a SortedList object. Syntax: public virtual int IndexOfValue (object value); Here, value is the Value which is to be located in the SortedList object. The value can be null. R 3 min read C# | Getting the keys in a SortedList object SortedList.Keys Property is used to get the keys in a SortedList object. Syntax: public virtual System.Collections.ICollection Keys { get; } Property Value: An ICollection object containing the keys in the SortedList object. Below programs illustrate the use of above-discussed property: Example 1: C 2 min read C# | Replacing the value at a specific index in a SortedList object SortedList.SetByIndex(Int32, Object) Method is used to replace the value at a specific index in a SortedList object. Syntax: public virtual void SetByIndex (int index, object value); Parameters: index: It is the zero-based index at which to save value. value: It is the Object to save into the Sorted 3 min read Like