c# - How to find an element from an array

Find an element from an Array
The Array class provides methods for creating, manipulating, searching, and sorting arrays. The Array class is not part of the System.Collections namespaces. However, it is still considered a collection because it is based on the IList interface. An element is a value in an Array. The length of an Array is the total number of elements it can contain. The Array has a fixed capacity.

The following .net c# tutorial code demonstrates how we can find an element from an Array instance. That means we will find the first element from an Array object which meet the conditions. In this .net c# tutorial code, we used Array Find() method to find an element from an Array instance.

The Array Find() method searches for an element that matches the conditions defined by the specified predicate and returns the first occurrence within the entire Array. The Array Find<T> (T[] array, Predicate<T> match) method has two parameters those are array and match. The array parameter is the one-dimensional, zero-based array to search and the match parameter is the predicate that defines the conditions of the element to search for.

The Array Find() method returns the first element that matches the conditions defined by the specified predicate if found otherwise it returns the default value for type T. The Find(array, match) method throws ArgumentNullException if the array is null or the match is null. So finally, using the Array Find() method .net developers can find an element (first element) from an Array instance that match the conditions.
array-find.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>  
  
<!DOCTYPE html>  
<script runat="server">  
    protected void Button1_Click(object sender, System.EventArgs e)  
    {
        string[] birds = new string[]
        {
            "Blue Duck",
            "Mandarin Duck",
            "Mallard",
            "Brown Teal",
            "Muscovy Duck"
        };

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

        //find only first element which match search query
        string startsWithB = Array.Find(birds, x => x.StartsWith("B"));
        string endsWithd = Array.Find(birds, x => x.EndsWith("d"));
        string endsWithduck = Array.Find(birds, x => x.EndsWith("duck",StringComparison.OrdinalIgnoreCase));

        Label1.Text += "<br />bird starts with [B]: " + startsWithB;
        Label1.Text += "<br />bird ends with [d]: " + endsWithd;
        Label1.Text += "<br />bird ends with [duck - ignore case]: " + endsWithduck;
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - array find</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:DarkBlue; font-style:italic;">  
            c# example - array find
        </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="test array find"  
            OnClick="Button1_Click"
            Height="40"  
            Font-Bold="true"  
            />  
    </div>  
    </form>  
</body>  
</html>
More c# examples