c# - How to check if a string array contains a value

Check string array contains a value
The following asp.net c# example code demonstrate us how can we determine whether a sting array contains a specified value programmatically at run time in an asp.net application. String array represent an array of string data type values.

.Net framework's Enumerable.Contains() method exists in System.Linq namespace. Enumerable.Contains() method allow us to determine whether a sequence contains a specified element. So, we can check whether a specified element exists in an array by using this Contains() method.

We just need to specify the source array object to search specified element within it and we also need to pass the specified element value as parameter of Contains() method to search this element within the entire array. Contains() method return 'true' if it found the specified element in string array; otherwise it return 'false'.

<%@ Page Language="C#" AutoEventWireup="true"%>  
<!DOCTYPE html>  
<script runat="server">  
    protected void Button1_Click(object sender, System.EventArgs e)  
        string[] birds = new string[]
            "Eurasian Bittern",
            "Little Bittern",
            "Striated Heron",
            "Goliath Heron",
            "Australian Pelican"

        Label1.Text = "birds array.........<br />";
        foreach (string s in birds)
            Label1.Text += s + "<br />";

        Boolean result = birds.Contains("Australian Pelican");
        Label1.Text += "<br />'Australian Pelican' bird contains in birds array? " + result;

        Boolean result2 = birds.Contains("Pygmy Cormorant");
        Label1.Text += "<br /><br />'Pygmy Cormorant' bird contains in birds array? " + result2;
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - check string array contains a value</title>  
    <form id="form1" runat="server">  
        <h2 style="color:DarkBlue; font-style:italic;">  
            c# example - check string array contains a value
        <hr width="550" align="left" color="LightBlue" />    
        <br /><br />
            Text="check string array contains a value"  
More c# examples