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

Two dimensional array - Get an element by index
TwoDimensionStringArrayIndex.aspx

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

<!DOCTYPE html>

<script runat="server">
    private string[,] colorArray = { { "Navy", "#000080" }, { "OldLace", "#FDF5E6" }, { "Olive", "#808000" }, { "OliveDrab", "#6B8E23" } };

    protected void Page_Load(object sender, System.EventArgs e) {
        if(!this.IsPostBack)
        {
            Label1.Text = "Two dimensional String array created successfully!<br />Array elements:<br /><br />";
            int rows = colorArray.GetUpperBound(0);
            int columns = colorArray.GetUpperBound(1);

            for (int currentRow = 0; currentRow <= rows; currentRow++)
            {
                for (int currentColumn = 0; currentColumn <= columns; currentColumn++)
                {
                    Label1.Text += colorArray[currentRow, currentColumn];
                    Label1.Text += " ";
                }
                Label1.Text += "<br />";
            }
        }
    }
    
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        Label2.Text = "Array [2,0] Value: " + colorArray[2,0];
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to get two dimension 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 />Two Dimension String Array Specific Index Value</h2>
        <asp:Label 
             ID="Label1" 
             runat="server" 
             Font-Size="Large"
             ForeColor="SaddleBrown"
             Font-Bold="true"
             Font-Italic="true"
             >
        </asp:Label>
        <br />
        <asp:Label 
             ID="Label2" 
             runat="server" 
             Font-Size="Large"
             ForeColor="SeaGreen"
             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,0] Index Position Value"
             ForeColor="DarkBlue"
             />   
    </div>
    </form>
</body>
</html>
More c# examples