Get the rank (number of dimensions) of the Array in CSharp

Description

The following code shows how to get the rank (number of dimensions) of the Array.

Example


//a one-dimensional array returns 1, 
//a two-dimensional array returns 2, and so on
using System;
// w  ww.  j  av a 2s.c om
public class Example
{
   public static void Main()
   {
      int[] array1 = new int[10];
      int[,] array2= new int[10,3];  
      int[][] array3 = new int[10][]; 

      Console.WriteLine("{0}: {1} dimension(s)", 
                        array1.ToString(), array1.Rank);
      Console.WriteLine("{0}: {1} dimension(s)", 
                        array2.ToString(), array2.Rank);
      Console.WriteLine("{0}: {1} dimension(s)", 
                        array3.ToString(), array3.Rank);
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var