c# - How to check if an int array contains a value

Int array contains
The following asp.net c# example code demonstrate us how can we determine whether an int array contains a specified element/number pogrammatically at run time in an asp.net application. .Net framework's int array represent an array of integer where each element value is an integer (Int32) number.

We can determine an element existence within a sequence by using Enumerable.Contains() method. We just need to specify the sequence (array) to locate the searched element and pass the searched element value to Contains() method to locate it in sequence.

So, we can determine whether an int array contains a specified integer number by calling Contains() method as this way IntArray.Contains(SpecifiedInteger). Contains() method return true if the specified integer number found in int array; otherwise it return 'false'.
int-array-contains.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>  
  
<!DOCTYPE html>  
<script runat="server">  
    protected void Button1_Click(object sender, System.EventArgs e)  
    {
        int[] marks = new int[]
        {
            82,
            75,
            58,
            42,
            65
        };

        Label1.Text = "marks array.........<br />";
        foreach (int i in marks)
        {
            Label1.Text += i.ToString() + "<br />";
        }

        Boolean result = marks.Contains(2);
        Label1.Text += "<br />'2' mark contains in marks array? " + result;

        Boolean result2 = marks.Contains(82);
        Label1.Text += "<br />'82' mark contains in marks array? " + result2;
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - int array contains</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:DarkBlue; font-style:italic;">  
            c# example - int array contains
        </h2>  
        <hr width="550" align="left" color="LightBlue" />    
  
        <asp:Label   
            ID="Label1"   
            runat="server"  
            Font-Size="X-Large"  
            >  
        </asp:Label>  
        <br /><br />
        <asp:Button   
            ID="Button1"   
            runat="server"   
            Text="check int array contains a value"  
            OnClick="Button1_Click"
            Height="40"  
            Font-Bold="true"  
            />  
    </div>  
    </form>  
</body>  
</html>
More c# examples