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.