c# - How to get the rank of an array

Get the number of dimensions of the array
.Net framework Array.Rank property allow us to get the number of dimensions (rank) of the array. the array Rank property exists in System namespace.

The array Rank property return value data type is System.Int32. this integer return value represents the rank (number of dimensions) of the array. for example, this property return 1, if the array is one-dimensional, it return 2 if array is two-dimensional and so on.

An array of arrays (jagged array) is a one-dimensional array. so its Rank property return 1. in this bellow examples we created few arrays with different dimensions. we display those arrays number of dimensions by calling array Rank property.

The following asp.net c# example code demonstrate us how can we get the number of array dimensions 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[] stringArray = new string[2];
        string[,] stringArray2 = new string[2, 5];
        string[,,] stringArray3 = new string[1, 1,1];

        int[,] intArray = new int[2, 3];
        int[,,] intArray2 = new int[1, 1,1];
        int[][] intArray3 = new int[5][];

        Label1.Text = "number of dimensions of the stringArray: " + stringArray.Rank.ToString();
        Label1.Text += "<br />number of dimensions of the stringArray2: " + stringArray2.Rank.ToString();
        Label1.Text += "<br />number of dimensions of the stringArray3: " + stringArray3.Rank.ToString();

        Label1.Text += "<br /><br />number of dimensions of the intArray: " + intArray.Rank.ToString();
        Label1.Text += "<br />number of dimensions of the intArray2: " + intArray2.Rank.ToString();
        Label1.Text += "<br />number of dimensions of the intArray3: " + intArray3.Rank.ToString();
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - array rank</title>  
    <form id="form1" runat="server">  
        <h2 style="color:DarkBlue; font-style:italic;">  
            c# example - array rank
        <hr width="550" align="left" color="LightBlue" />    
        <br /><br />
            Text="array rank"  
More c# examples