Generic List LastIndexOf() method in asp.net c#

Generic List LastIndexOf() Method
.Net framework generic list LastIndexOf(T) method search for the specified object and return the zero-based index of the last occurrence within the entire List<T>. this LastIndexOf(T) method exists under System.Collections.Generic namespace. the LastIndexOf(T) method has a required parameter named 'item'. this parameter type is 'T'. the 'T' indicate the object to locate in the List<T> and the value can be null for reference types.

LastIndexOf(T) method return value data type is System.Int32. return value is the zero-based index of the last occurrence of 'item' within the entire List<T>, if the element found. the method return -1, if the 'item' not found in List<T>. if the list contains multiple elements of same name then it return the index of last occurrence.

the following asp.net c# example code demonstrate us how can we get the index of specified element's last occurrence from a generic list in an asp.net application.
GenericListLastIndexOfMethod.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        List<string> colors = new List<string>() {"DarkSlateBlue","Green","White"};
        colors.Add("Snow");
        colors.Add("Green");

        Label1.Text = "List Elements....<br />";
        foreach (string color in colors)
        {
            Label1.Text += "<br />" + color;
        }

        Label1.Text += "<br /><br />LastIndexOf color 'Green': " + colors.LastIndexOf("Green");
        Label1.Text += "<br /><br />LastIndexOf color 'Snow': " + colors.LastIndexOf("Snow");
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Generic List LastIndexOf() - How to get the specific List element's last occurrence index</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:DarkSlateGray; font-style:italic;">
            System.Collections.Generic.List LastIndexOf() Method
            <br /> How to get the specific List element's last occurrence index
        </h2>
        <hr width="525" align="left" color="SlateBlue" />
        <asp:Label
             ID="Label1"
             runat="server"
             ForeColor="Magenta"
             Font-Size="Large"
             Font-Names="Courier New"
             >
        </asp:Label>
        <br /><br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Test Generic List LastIndexOf() Method"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>
Related asp.net examples