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

Get an element by index from an array
The following asp.net c# example code demonstrate us how can we get an array element value by its index number programmatically at run time in an asp.net application. .Net framework's array contain zero-based index. So, array index value zero (0) represent the first element of an array, index number 1 represent the second element of the array and so on.

We can access/get an array element's value by accessing its index number as array[index]. This array[0] code return the array fist element value, array[1] allow us to get the array second element value and so on. So, in this way we can get an array's any element value by using its index number.
StringArrayIndex.aspx

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

<!DOCTYPE html>

<script runat="server">
    private string[] controls = { "ListView", "GridView", "DetailsView", "FormView", "LinqDataSource", "EntityDataSource" };
    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)
    {
        Label2.Text = "Array [2] Index Value: " + controls[2];
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to get string array specific index position value in asp.net</title>
</head> 
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Green">asp.net array example:<br />String Array Specific Index Positin Value</h2>
        <asp:Label 
             ID="Label1" 
             runat="server" 
             Font-Size="Large"
             ForeColor="HotPink"
             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="Get Array [2] Value"
             ForeColor="SeaGreen"
             />   
    </div>
    </form>
</body>
</html>
More c# examples