Array exists
The following asp.net c# example code demonstrate us how can we
determine whether specified elements are exists in anarray based on
search criteria. .Net framework's Array Class Array.Exists<T>() method
allow us to determine whetherthe specified array contains one or more
elements that match the conditions defined by the specified predicate.
Array.Exists<T>() method type parameter is 't' which represent the type of the elements of the array. This method hastwo required parameters named 'array' and 'match'. The 'array' parameter represent the one-dimensional, zero-based index array tosearch. The 'match' parameter value represent the Predicate<T> that defines the conditions of the elements to search for.
Array.Exists() method return a Boolean value. It return 'true', if array contains value that match the condition; otherwiseit return 'false'.
In this example code, we call the Array.Exists() method three times. First and second call, we check whether array contain anelement with value 'skyblue' and 'green.' Third call, we search the array for elements whose value ends with character 'd'.
Array.Exists<T>() method type parameter is 't' which represent the type of the elements of the array. This method hastwo required parameters named 'array' and 'match'. The 'array' parameter represent the one-dimensional, zero-based index array tosearch. The 'match' parameter value represent the Predicate<T> that defines the conditions of the elements to search for.
Array.Exists() method return a Boolean value. It return 'true', if array contains value that match the condition; otherwiseit return 'false'.
In this example code, we call the Array.Exists() method three times. First and second call, we check whether array contain anelement with value 'skyblue' and 'green.' Third call, we search the array for elements whose value ends with character 'd'.
array-exists.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
string[] colors = new string[]
{
"blue",
"darkblue",
"skyblue",
"deepblue",
"red"
};
Label1.Text = "colors array.........<br />";
foreach (string s in colors)
{
Label1.Text += s + "<br />";
}
Boolean skyblueExists = Array.Exists(colors, x => x == "skyblue");
Boolean greenExists = Array.Exists(colors, x => x == "green");
Boolean colorEndEithd = Array.Exists(colors, x=> x.EndsWith("d"));
Label1.Text += "<br />skyblue color exists in color array? " + skyblueExists.ToString();
Label1.Text += "<br />green color exists in color array? " + greenExists.ToString();
Label1.Text += "<br />any color end with 'd' exists in color array? " + colorEndEithd.ToString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>c# example - array exists</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:DarkBlue; font-style:italic;">
c# example - array exists
</h2>
<hr width="550" align="left" color="LightBlue" />
<asp:Label
ID="Label1"
runat="server"
Font-Size="Large"
>
</asp:Label>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
Text="test array exists"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>