C# | Getting the keys in a SortedList object Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: CSharp // C# code to get an ICollection containing // the keys in the SortedList using System; using System.Collections; class GFG { // Driver code public static void Main() { // Creating a SortedList SortedList mylist = new SortedList(); // Adding elements in SortedList mylist.Add("4", "Even"); mylist.Add("9", "Odd"); mylist.Add("5", "Odd and Prime"); mylist.Add("2", "Even and Prime"); // Get a collection of the keys. ICollection c = mylist.Keys; // Displaying the contents foreach(string str in c) Console.WriteLine(str + ": " + mylist[str]); } } Output: 2: Even and Prime 4: Even 5: Odd and Prime 9: Odd Example 2: CSharp // C# code to get an ICollection containing // the keys in the SortedList. using System; using System.Collections; class GFG { // Driver code public static void Main() { // Creating a SortedList SortedList mylist = new SortedList(); // Adding elements in SortedList mylist.Add("India", "Country"); mylist.Add("Chandigarh", "City"); mylist.Add("Mars", "Planet"); mylist.Add("China", "Country"); // Get a collection of the keys. ICollection c = mylist.Keys; // Displaying the contents foreach(string str in c) Console.WriteLine(str + ": " + mylist[str]); } } Output: Chandigarh: City China: Country India: Country Mars: Planet Note: The ICollection object is a read-only view of the keys of the SortedList object. Modifications made to the underlying SortedList are immediately reflected in the ICollection. The elements of the ICollection are sorted in the same order as the keys of the SortedList. This property is similar to the GetKeyList method but returns an ICollection object instead of an IList object. This method is an O(1) operation. Reference: https://learn.microsoft.com/en-us/dotnet/api/system.collections.sortedlist.keys?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Getting the list of keys of a SortedList object K Kirti_Mangal Follow Improve Article Tags : C# CSharp-Collections-Namespace CSharp-Collections-SortedList Similar Reads 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 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 list of keys of a SortedList object 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: Exam 2 min read C# | Getting the list of keys of a SortedList object 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: Exam 2 min read 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 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 Like