C# | Check if ListDictionary contains a specific key Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 specified key, otherwise it returns false. Exception: This method will give ArgumentNullException if the key is null. Below given are some examples to understand the implementation in a better way: Example 1: CSHARP // C# code to check if ListDictionary // contains a specific key using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // Creating a ListDictionary named myDict ListDictionary myDict = new ListDictionary(); myDict.Add("Australia", "Canberra"); myDict.Add("Belgium", "Brussels"); myDict.Add("Netherlands", "Amsterdam"); myDict.Add("China", "Beijing"); myDict.Add("Russia", "Moscow"); myDict.Add("India", "New Delhi"); // Checking if ListDictionary contains // the key "Brazil" Console.WriteLine(myDict.Contains("Brazil")); } } Output: False Example 2: CSHARP // C# code to check if ListDictionary // contains a specific key using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // Creating a ListDictionary named myDict ListDictionary myDict = new ListDictionary(); myDict.Add("Australia", "Canberra"); myDict.Add("Belgium", "Brussels"); myDict.Add("Netherlands", "Amsterdam"); myDict.Add("China", "Beijing"); myDict.Add("Russia", "Moscow"); myDict.Add("India", "New Delhi"); // Checking if ListDictionary contains // the key "null". This should raise // "ArgumentNullException" as the key is null Console.WriteLine(myDict.Contains(null)); } } Runtime Error: Unhandled Exception: System.ArgumentNullException: Key cannot be null. Parameter name: key Note: This method is an O(n) operation, where n is Count. Reference: https://learn.microsoft.com/en-us/dotnet/api/system.collections.specialized.listdictionary.contains?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Check if OrderedDictionary collection contains a specific key S Sahil_Bansall Follow Improve Article Tags : C# CSharp-method CSharp-Specialized-Namespace CSharp-Specialized-ListDictionary Similar Reads C# | Check if OrderedDictionary collection contains a specific key OrderedDictionary.Contains(Object) method is used to check whether the OrderedDictionary collection contains a specific key or not. Syntax: public bool Contains (object key); Here, key is the key to locate in the OrderedDictionary collection. Return Value: This method returns True if the OrderedDict 2 min read C# | Check if OrderedDictionary collection contains a specific key OrderedDictionary.Contains(Object) method is used to check whether the OrderedDictionary collection contains a specific key or not. Syntax: public bool Contains (object key); Here, key is the key to locate in the OrderedDictionary collection. Return Value: This method returns True if the OrderedDict 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 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 C# | Check if the StringDictionary contains a specific key 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 conta 2 min read C# | Check if the StringDictionary contains a specific key 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 conta 2 min read Like