Generic List Contains() method in asp.net c#

Generic List Contains() Method
.Net framework generic list Contains() method allow us to determine whether an element exists in the List<T>. the Contains(T) method exists under System.Collections.Generic namespace. this method require to pass a parameter named 'item'. this parameter value type is 'T'. this 'T' represents the object to find in the generic List<T>. this value can be null for reference types.

this Contains(T) method return value data type is System.Boolean. return value 'true' indicate the specified item is found in the List<T>. otherwise the method return 'false'. this method implements as ICollection<T>.Contains(T).

the following asp.net c# example code demonstrate us how can we determine whether an element exists in generic list in an asp.net application.
GenericListContainsMethod.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>

<!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)
    {
        List<string> colors = new List<string>();
        colors.Add("Chocolate");
        colors.Add("Coral");
        colors.Add("Cyan");
        colors.Add("DarkGoldenRod");

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

        Label1.Text += "<br /><br />Green Exists in List? " + colors.Contains("Green");
        Label1.Text += "<br />Cyan Exists in List? " + colors.Contains("Cyan");
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Generic List Contains() - How to check an element is exists in the List</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:DarkOrchid; font-style:italic;">
            System.Collections.Generic.List Contains() method
            <br /> How to check an element is exists in the List
        </h2>
        <hr width="480" align="left" color="Orchid" />
        <asp:Label
             ID="Label1"
             runat="server"
             Font-Size="Large"
             ForeColor="DarkBlue"
             >
        </asp:Label>
        <br /><br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Test Generic List Contains() Method"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>
Related asp.net examples