C# | Getting the list of keys of a SortedList object Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report SortedList.GetKeyList Method is used to get the list of keys in a SortedList object. Syntax: public virtual System.Collections.IList GetKeyList (); Return Value: It returns an IList object containing the keys in the SortedList object. Below programs illustrate the use of above-discussed method: Example 1: CSharp // C# code for getting the keys // in 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("1", "C++"); mylist.Add("2", "Java"); mylist.Add("3", "DSA"); mylist.Add("4", "Python"); mylist.Add("5", "C#"); // taking an IList and // using GetKeyList method IList klist = mylist.GetKeyList(); // Prints the list of keys Console.WriteLine("Key List"); for (int i = 0; i < mylist.Count; i++) Console.WriteLine(klist[i]); } } Output: Key List 1 2 3 4 5 Example 2: CSharp // C# code for getting the keys // in 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"); // taking an IList and // using GetKeyList method IList klist = mylist.GetKeyList(); // Prints the list of keys Console.WriteLine("Key List"); // will print the keys in sorted order for (int i = 0; i < mylist.Count; i++) Console.WriteLine(klist[i]); } } Output: Key List Fifth First Fourth Second Third Note: The returned IList object is a read-only view of the keys of the SortedList object. Modifications made to the underlying SortedList are immediately reflected in the IList. The elements of the returned IList are sorted in the same order as the keys of the SortedList. This method is similar to the Keys property but returns an IList object instead of an ICollection object. This method is an O(1) operation. Reference: https://learn.microsoft.com/en-us/dotnet/api/system.collections.sortedlist.getkeylist?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Getting the Values in a SortedList object K Kirti_Mangal Follow Improve Article Tags : C# CSharp-method CSharp-Collections-Namespace CSharp-Collections-SortedList Similar Reads C# | Getting the list of Values of a SortedList object SortedList.GetValueList Method is used to get the list of keys in a SortedList object. Syntax: public virtual System.Collections.IList GetValueList (); Return Value: It returns an IList object containing the values in the SortedList object. Below programs illustrate the use of above-discussed method 2 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# | Getting the Values in a SortedList object SortedList.Values Property is used to get the values in a SortedList object. Syntax: public virtual System.Collections.ICollection Values { get; } Property Value: An ICollection object containing the values in the SortedList object. Below programs illustrate the use of above-discussed property: Exam 2 min read C# | Getting the key at the specified index of a SortedList object 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. Ex 2 min read 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 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 Like