c# - Find index of last element from an array that match the conditions

Array find last index
The following asp.net c# example code demonstrate us, how can we get index of last occurrence of an element within an array which elements are matches the search condition, programmatically at run time in an asp.net application.

.Net framework's Array Class Array.FindLastIndex() method searches for an element that matches the condition defined by a specified predicate, return the zero-based index of the last occurrence within an array or a portion of array. Array.FindLastIndex() method has few overloads.

ArrayFindLastIndex<T>(T[], Predicate<T>) overloaded method searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the last occurrence within the entire array.

ArrayFindLastIndex(T[], Predicate<T>) method has two required parameters named 'array' and 'match'. The 'array' parameter represent the one-dimensional, zero-based array to search and 'match' parameter represent the Predicate<T> that defines the conditions of the elements to search for.

ArrayFindLastIndex() method return an integer data type value which represent the zero-based index of the last occurrence of an element that matches the conditions defined by 'match' parameter; otherwise it return -1.
array-findlastindex.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>  
  
<!DOCTYPE html>  
<script runat="server">  
    protected void Button1_Click(object sender, System.EventArgs e)  
    {
        string[] birds = new string[]
        {
            "Greater Painted Snipe",
            "Northern Lapwing",
            "Hudsonian Godwit",
            "Masked Lapwing",
            "Black Oystercatcher"
        };

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

        int lastindexofLapwing = Array.FindLastIndex(birds, x => x.EndsWith("Lapwing"));
        Label1.Text += "<br />last index of [Lapwing] in birds array: " + lastindexofLapwing.ToString();
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - array findlastindex</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:DarkBlue; font-style:italic;">  
            c# example - array findlastindex
        </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="array findlastindex"  
            OnClick="Button1_Click"
            Height="40"  
            Font-Bold="true"  
            />  
    </div>  
    </form>  
</body>  
</html>
More c# examples