C# | Check if the StringDictionary contains a specific key Last Updated : 01 Feb, 2019 Comments Improve Suggest changes Like Article Like Report StringDictionary.ContainsKey(String) method is used to check whether the StringDictionary contains a specific key or not. Syntax: public virtual bool ContainsKey (string key); Here, key is the key to locate in the StringDictionary. Return Value: This method returns true if the StringDictionary contains an entry with the specified key, otherwise it returns the false. Exception: This method will give ArgumentNullException if the key is null. Below programs illustrate the use of StringDictionary.ContainsKey(String) method: Example 1: CSHARP // C# code to check if the // StringDictionary contains // a specific key using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // Creating a StringDictionary named myDict StringDictionary myDict = new StringDictionary(); // Adding key and value into the StringDictionary myDict.Add("G", "Geeks"); myDict.Add("F", "For"); myDict.Add("C", "C++"); myDict.Add("DS", "Data Structures"); myDict.Add("N", "Noida"); // Checking if "DS" is contained in // StringDictionary myDict if (myDict.ContainsKey("DS")) Console.WriteLine("StringDictionary myDict contains the key"); else Console.WriteLine("StringDictionary myDict does not contain the key"); } } Output: StringDictionary myDict contains the key Example 2: CSHARP // C# code to check if the // StringDictionary contains // a specific key using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // Creating a StringDictionary named myDict StringDictionary myDict = new StringDictionary(); // Adding key and value into the StringDictionary myDict.Add("G", "Geeks"); myDict.Add("F", "For"); myDict.Add("C", "C++"); myDict.Add("DS", "Data Structures"); myDict.Add("N", "Noida"); // Checking if "null" is contained in // StringDictionary myDict // This should raise "ArgumentNullException" // as the key is null if (myDict.ContainsKey(null)) Console.WriteLine("StringDictionary myDict contains the key"); else Console.WriteLine("StringDictionary myDict does not contain the key"); } } Runtime Error: Unhandled Exception: System.ArgumentNullException: Value cannot be null. Parameter name: key Note: The key is handled in a case-insensitive manner i.e, it is translated to lowercase before it is added to the string dictionary. This method is an O(1) operation. Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.specialized.stringdictionary.containskey?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Check if the StringDictionary contains a specific key S Sahil_Bansall Follow Improve Article Tags : C# CSharp-method CSharp-Collections-Namespace CSharp-Specialized-StringDictionary CSharp-Specialized-Namespace +1 More Similar Reads C# | Check if the StringDictionary contains a specific value StringDictionary.ContainsValue(String) method is used to check whether the StringDictionary contains a specific value or not. Syntax: public virtual bool ContainsValue (string value); Here, value is the value to locate in the StringDictionary. The value can be null. Return Value: The method returns 2 min read C# | Check if ListDictionary contains a specific key ListDictionary.Contains(Object) method is used to check whether the ListDictionary contains a specific key or not. Syntax: public bool Contains (object key); Here, key is the key to locate in the ListDictionary. Return Value: The method returns true if the ListDictionary contains an entry with the s 2 min read C# | Check if the Hashtable contains a specific Key The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Hashtable.ContainsKey(Object) Method is used to check whether the Hashtable contains a specific key or not. Syntax: public v 2 min read C# | Get a collection of keys in the StringDictionary StringDictionary.Keys property is used to get a collection of keys in the StringDictionary. Syntax: public virtual System.Collections.ICollection Keys { get; } Return Value: An ICollection that provides the keys in the StringDictionary. Below given are some examples to understand the implementation 2 min read C# | Check if SortedDictionary contains the specified key or not SortedDictionary<TKey, TValue>.ContainsKey(TKey) Method is used to check whether the SortedDictionary contains an element with the specified key or not. Syntax: public bool ContainsKey (TKey key); Here, the key is the Key which is to be located in the SortedDictionary. Returns Value: This meth 2 min read Like