C# | Dictionary.ContainsKey() Method Last Updated : 01 Feb, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report This method is used to check whether the Dictionary<TKey,TValue> contains the specified key or not. Syntax: public bool ContainsKey (TKey key); Here, the key is the Key which is to be located in the Dictionary. Return Value: This method will return true if the Dictionary contains an element with the specified key otherwise, it returns false. Exception: This method will give ArgumentNullException if the key is null. Below are the programs to illustrate the use of Dictionary<TKey,TValue>.ContainsKey() Method: Example 1: csharp // C# code to check if a key is // present or not in a Dictionary. using System; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Create a new dictionary of // strings, with string keys. Dictionary<string, string> myDict = new Dictionary<string, string>(); // Adding key/value pairs in myDict 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 for Key "India" if (myDict.ContainsKey("India")) Console.WriteLine("Key : India is present"); else Console.WriteLine("Key : India is absent"); } } Output: Key : India is present Example 2: csharp // C# code to check if a key is // present or not in a Dictionary. using System; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Create a new dictionary of // strings, with string keys. Dictionary<string, string> myDict = new Dictionary<string, string>(); // Adding key/value pairs in myDict 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 for Key "USA" if (myDict.ContainsKey("USA")) Console.WriteLine("Key : USA is present"); else Console.WriteLine("Key : USA is absent"); } } Output: Key : USA is absent Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.containskey?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Dictionary.ContainsKey() Method R rupesh_rao Follow Improve Article Tags : C# CSharp-method CSharp-Generic-Namespace CSharp Dictionary Class Similar Reads C# | Dictionary.ContainsValue() Method This method is used to check whether the Dictionary<TKey,TValue> contains a specific value or not. Syntax: public bool ContainsValue (TValue value); Here, the value is the Value to locate in the Dictionary. The value can be null for reference types. Return Value: This method returns true if th 2 min read C# | Dictionary.Add() Method Dictionary<TKey,TValue>.Add() Method is used to add a specified key and value to the dictionary. Syntax: public void Add (TKey key, TValue value); Parameters: key: It is the key of the element to add. value: It is the value of the element to add. The value can be null for reference types. Exce 3 min read C# | Dictionary.Count Property This property is used to get the number of key/value pairs contained in the Dictionary. Syntax: public int Count { get; } Return Value : The number of key/value pairs contained in the Dictionary. Below are the programs to illustrate the use of above-discussed property: Example 1: csharp // C# code t 2 min read C# | Dictionary.Remove Method This method is used to remove the value with the specified key from the Dictionary<TKey,TValue>. Syntax: public bool Remove (TKey key); Return Value: This method returns true if the element is successfully found and removed; otherwise it returns false. This method returns false if key is not f 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 Like