Dictionary ContainsKey() Method
.Net framework Dictionary<TKey, TValue>.ContainsKey() method allow us to determine whether the Dictionary<TKey, TValue>
contains the specific key. the ContainsKey() method exists under System.Collections.Generic namespace.
this method require to pass a parameter named 'key'. this 'key' parameter value type is 'TKey' which represents the key to locate in the Dictionary<TKey, TValue>.
ContainsKey() method return value data type is System.Boolean. method return 'true', if the Dictionary<TKey, TValue> contains an element with the specified key. otherwise, the method return false.
this method implements as IDictionary<TKey, TValue>.ContainsKey(TKey) and IReadOnlyDictionary<TKey, TValue>.ContainsKey(TKey). the ContainsKey() method throw ArgumentNullException exception if the 'key' is null.
the following asp.net c# example code demonstrate us how can we determine whether the dictionary contains an element with the specified key programmatically at run time in an asp.net application.
this method require to pass a parameter named 'key'. this 'key' parameter value type is 'TKey' which represents the key to locate in the Dictionary<TKey, TValue>.
ContainsKey() method return value data type is System.Boolean. method return 'true', if the Dictionary<TKey, TValue> contains an element with the specified key. otherwise, the method return false.
this method implements as IDictionary<TKey, TValue>.ContainsKey(TKey) and IReadOnlyDictionary<TKey, TValue>.ContainsKey(TKey). the ContainsKey() method throw ArgumentNullException exception if the 'key' is null.
the following asp.net c# example code demonstrate us how can we determine whether the dictionary contains an element with the specified key programmatically at run time in an asp.net application.
DictionaryContainsKeyMethod.aspx
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Collections.Generic" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
Dictionary<string, string> colors = new Dictionary<string, string>();
colors.Add("Gold", "#FFD700");
colors.Add("Gainsboro", "#DCDCDC");
colors.Add("GhostWhite", "#F8F8FF");
colors.Add("DodgerBlue", "#1E90FF");
Label1.Text = "Dictionary Keys...<br />";
foreach (string color in colors.Keys)
{
Label1.Text += color + "<br />";
}
//ContainsKey() method check Dictionary contains the specified key.
Label1.Text += "<br />'DodgerBlue' Key Exists In Dictionary? " + colors.ContainsKey("DodgerBlue");
Label1.Text += "<br />'Green' Key Exists In Dictionary? " + colors.ContainsKey("Green");
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Dictionary ContainsKey() Method - How to check Dictionary contains the specified key</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Navy; font-style:italic;">
System.Collections.Generic.Dictionary ContainsKey() Method
<br /> How to check Dictionary contains the specified key
</h2>
<hr width="550" align="left" color="Navy" />
<br />
<asp:Label
ID="Label1"
runat="server"
ForeColor="OrangeRed"
Font-Names="Courier New"
Font-Size="Large"
>
</asp:Label>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
OnClick="Button1_Click"
Text="Test Dictionary ContainsKey() Method"
Height="45"
Font-Bold="true"
ForeColor="DodgerBlue"
/>
</div>
</form>
</body>
</html>

- How to add an item to a Dictionary
- How to remove all items from a Dictionary
- How to check whether Dictionary contains a specific value
- How to sort a Dictionary by value in descending order
- How to sort a Dictionary in descending order
- How to use Dictionary TryGetValue
- How to make Dictionary TryGetValue case insensitive
- How to remove first element from Dictionary
- How to remove items from Dictionary in foreach loop
- How to remove items from a Dictionary while iterating