C# | Getting the keys in a SortedList object Last Updated : 01 Feb, 2019 Comments Improve Suggest changes 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://docs.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 keys in 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 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 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 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# | Check if a SortedList object is synchronized SortedList class is a collection of (key, value) pairs which are sorted according to keys. Those pairs can be accessible by key and as well as by index(zero-based indexing). This comes under System.Collections namespace. SortedList.IsSynchronized property is used to get a value indicating whether ac 2 min read C# | Copying the SortedList elements to an Array Object SortedList.CopyTo(Array, Int32) Method is used to copy SortedList elements to a one-dimensional Array object, starting at the specified index in the array. Syntax: public virtual void CopyTo (Array array, int arrayIndex); Parameters: array: It is the one-dimensional Array object that is the destinat 2 min read C# | Check whether a SortedList object contains a specific key SortedList.Contains(Object) Method is used to check whether a SortedList object contains a specific key. Syntax: public virtual bool Contains (object key); Here, key is the Key which is to be located in the SortedList object. Return Value: This method returns the true if the SortedList object contai 2 min read Like