c# - How to get value of a specified element of an array

Array get value
The following asp.net c# example code demonstrate us how can we get a specified element value from an array object programmatically at run time in an asp.net application. .Net framework's Array Class Array.GetValue() method get the value of the specified element in the current array.

Array.GetValue(Int32) overloaded method allow us to get the value at the specified position in the one-dimensional array. In this example, we uses this overloaded member. GetValue(Int32) method has a required parameter named 'index' which value data type is System.Int32. This parameter value contains a 32-bit integer that represent the position of the array element to get.

Array.GetValue(Int32) method return a System.Object type value. Method return the element value at the specified position in the one dimensional array.

So, to get a specified element value of an one-dimensional array by index number, we can call the GetValue(Int32) method as this way Array.GetValue(ElementIndex). Finally, we get the specified element value.

<%@ Page Language="C#" AutoEventWireup="true"%>  
<!DOCTYPE html>  
<script runat="server">  
    protected void Button1_Click(object sender, System.EventArgs e)  
        string[] birds = new string[]
            "Indian Robin",
            "Northern Wheatear",
            "Spotted Flycatcher",
            "Thrush Nightingale"

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

        string index1bird = birds.GetValue(1).ToString();
        string index3bird = birds.GetValue(3).ToString();

        Label1.Text += "<br />birds array index 1 bird is: " + index1bird;
        Label1.Text += "<br />birds array index 3 bird is: " + index3bird;
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - array getvalue</title>  
    <form id="form1" runat="server">  
        <h2 style="color:DarkBlue; font-style:italic;">  
            c# example - array getvalue
        <hr width="550" align="left" color="LightBlue" />    
        <br /><br />
            Text="array getvalue"  
More c# examples