c# - How to check whether Dictionary contains a specific value

Dictionary ContainsValue() Method
.Net framework Dictionary represents a collection of keys and values. Dictionary class exists under System.Collections.Generic namespace.

Dictionary<TKey, TValue> ContainsValue() method allow us to determine whether the dictionary contains a specific value. this ContainsValue() method require to pass a parameter named 'value'. this 'value' parameter value type is 'TValue' which represents the value to locate in the Dictionary<TKey, TValue> and the value can be null for reference types.

this method return value data type is System.Boolean. the ContainsValue() method return 'true', if the Dictionary<TKey, TValue> contains an element with the specified value. otherwise, this method return 'false'.

the following asp.net c# example code demonstrate us how can we check whether dictionary contains an element with the specified value programmatically at run time in an asp.net application.
DictionaryContainsValueMethod.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Collections.Generic" %>

<!DOCTYPE html>
<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        Dictionary<string, string> colors = new Dictionary<string, string>();
        colors.Add("#F0E68C", "Khaki");
        colors.Add("#7CFC00", "LawnGreen");
        colors.Add("#000080", "Navy");
        colors.Add("#4B0082", "Indigo");

        Label1.Text = "Dictionary Keys...<br />";

        foreach (string color in colors.Values)
        {
            Label1.Text += color + "<br />";
        }

        //ContainsValue() method check whether the Dictionary contains a specific value.
        Label1.Text += "<br />'Indigo' Value Exists In Dictionary? " + colors.ContainsValue("Indigo");
        Label1.Text += "<br />'Orange' Value Exists In Dictionary? " + colors.ContainsValue("Orange");
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Dictionary ContainsValue() Method - How to check whether Dictionary contains a specific value</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Navy; font-style:italic;">
            System.Collections.Generic.Dictionary ContainsValue() Method
            <br /> How to check whether Dictionary contains a specific value
        </h2>
        <hr width="550" align="left" color="Navy" />
        <br />
        <asp:Label
             ID="Label1"
             runat="server"
             ForeColor="Indigo"
             Font-Names="Courier New"
             Font-Size="Large"
             >
        </asp:Label>
        <br /><br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Test Dictionary ContainsValue() Method"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>
More c# examples