C# | Check whether a SortedList object contains a specific key Last Updated : 01 Feb, 2019 Comments Improve Suggest changes Like Article Like Report 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 contains an element with the specified key otherwise, it returns false Exceptions: ArgumentNullException: If the key is null. InvalidOperationException: If the comparer throws an exception. Below programs illustrate the use of above-discussed method: Example 1: CSharp // C# code to Check whether a SortedList // object contains a specific key 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#"); // Checking whether 4 is present // in SortedList or not Console.Write(mylist.Contains("4")); } } Output: True Example 2: CSharp // C# code to Check whether a SortedList // object contains a specific key 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"); // Checking whether 10 is present // in SortedList object or not Console.Write(mylist.Contains("Sixth")); } } Output: False Note: Contains implements IDictionary.Contains. It behaves exactly as ContainsKey. This method uses a binary search algorithm; therefore, this method is an O(log n) operation, where n is Count. Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.sortedlist.contains?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Check whether a SortedList object contains a specific key K Kirti_Mangal Follow Improve Article Tags : C# CSharp-method CSharp-Collections-Namespace CSharp-Collections-SortedList Similar Reads C# | Check if a SortedList object contains a specific value 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.ContainsValue(Object) method is used to check whether a SortedList o 2 min read C# | Check whether a Hashtable contains a specific key or not Hashtable.Contains(Object) Method is used to check whether the Hashtable contains a specific key or not. Syntax: public virtual bool Contains (object key); Here, key is the Key of Object type which is to be located in the Hashtable. Return Value: This method returns true if the Hashtable contains an 2 min read C# | Check if the SortedSet contains a specific element SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet<T>.Contains(T) Method is used to check if a SortedSet contains a specific element or not. Properties: In C#, SortedSet class can be used to store, remov 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 Like