How to define the rank of an array in C#?


To find the number of dimensions of an array, use the Array Rank property. This is how you can define it −

arr.Rank

Here, arr is our array −

int[,] arr = new int[3,4];

If you want to get the rows and columns it has, then uses the GetLength property −

arr.GetLength(0);
arr.GetLength(1);

The following is the complete code −

Example

 Live Demo

using System;

class Program {
   static void Main() {
      int[,] arr = new int[3,4];

      Console.WriteLine(arr.GetLength(0));
      Console.WriteLine(arr.GetLength(1));

      // Length
      Console.WriteLine(arr.Length);
      Console.WriteLine("Dimensions of Array : " + arr.Rank);
   }
}

Output

3
4
12
Dimensions of Array : 2

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 20-Jun-2020

290 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements