c# - How to get index of an element from an array

Array IndexOf() Method
The following asp.net c# example code demonstrate us how can we get index of an array element programmatically at run time in an asp.net application. Array Class Array.IndexOf(Array, Object) overloaded method search for the specified object and return the index of its first occurrence in a one-dimensional array.

Array.IndexOf(Array, Object) method has two required parameters named 'array' and 'value'. The 'array' parameter represent the one-dimensional array to search and 'value' parameter represent the object to locate in array. In this example code, we initializes a string array with elements and pass a string object as 'value' parameter value to IndexOf() method to get its index.

Array.IndexOf() method return an integer value which represent the index of the first occurrence of specified object in array, if found; otherwise it return the lower bound of the array minus 1. The method throw RankException exception, if the array is multidimensional. Array contain zero-based index.
ArrayIndexOf.aspx

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

<!DOCTYPE html>

<script runat="server">
    private string[] controls = { "SqlDataSource", "AccessDataSource", "ObjectDataSource", "XmlDataSource", "LinqDataSource", "EntityDataSource", "SiteMapDataSource" };
    protected void Page_Load(object sender, System.EventArgs e) {
        if(!this.IsPostBack)
        {
            Label1.Text = "String array created successfully!<br />Array elements:<br /><br />";
            foreach (string element in controls)
            {
                Label1.Text += element + "<br />";
            }
        }

    }
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        int indexOf = Array.IndexOf(controls, "LinqDataSource");
        Label2.Text ="We Find [LinqDataSource] At the index position: " + indexOf.ToString();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to get (search, find) array index position by array element value in asp.net(Array.IndexOf)</title>
</head> 
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Red">asp.net Array.IndexOf() example:<br />Search Array Index Number By Element Value</h2>
        <asp:Label 
             ID="Label1" 
             runat="server" 
             Font-Size="Large"
             ForeColor="SeaGreen"
             Font-Bold="true"
             Font-Italic="true"
             >
        </asp:Label>
        <br />
        <asp:Label 
             ID="Label2" 
             runat="server" 
             Font-Size="Large"
             ForeColor="DodgerBlue"
             Font-Bold="true"
             Font-Italic="true"
             >
        </asp:Label>
        <br /><br />
        <asp:Button 
             ID="Button1" 
             runat="server" 
             OnClick="Button1_Click"
             Font-Bold="true"
             Text="Search Array [LinqDataSource] Index Position"
             ForeColor="SeaGreen"
             />   
    </div>
    </form>
</body>
</html>
More c# examples