c# - How to get the length of an array

Array get length
.Net framework Array.Length property allow us to get a 32-bit integer that represents the total number of elements in all the dimension of the Array. this array Length property exists in System namespace.

the array Length property return value data type is System.int32. this integer value represents the total number of elements in all the dimension of the array. this property return zero, if there are no elements exists in the array.

array Length property throw OverFlowException exception, if the array is multidimensional and contains more than MaxValue elements. MaxValue represents the largest possible value of an Int32. this field is constant.

the following asp.net c# example code demonstrate us how can we get an array length programmatically at run time in an asp.net application.

<%@ Page Language="C#" AutoEventWireup="true"%>  
<!DOCTYPE html>  
<script runat="server">  
    protected void Button1_Click(object sender, System.EventArgs e)  
        string[,] birds = new string[5, 2]

        int rows = birds.GetUpperBound(0);
        int columns = birds.GetUpperBound(1);

        //get number of elements exists in first dimension
        int firstDimensionLength = birds.GetLength(0);

        //get number of elements exists in second dimension
        int secondDimensionLength = birds.GetLength(1);

        Label1.Text = "first dimension[0] length: " + firstDimensionLength.ToString();
        Label1.Text += "<br />second dimension[1] length: " + secondDimensionLength.ToString();

        Label1.Text += "<br /><br />two dimension birds array elements.........<br />";
        for (int currentRow = 0; currentRow <= rows;currentRow++ )
            for (int currentColumn = 0; currentColumn <= columns;currentColumn++ )
                Label1.Text += birds[currentRow, currentColumn] + " ";
            Label1.Text += "<br />";
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - array getlength</title>  
    <form id="form1" runat="server">  
        <h2 style="color:DarkBlue; font-style:italic;">  
            c# example - array getlength
        <hr width="550" align="left" color="LightBlue" />    
        <br />
            Text="array getlength"  
More c# examples