How to find the rank of an array in C#
Last Updated :
23 Jan, 2019
Improve
Array.Rank Property is used to get the rank of the Array. Rank is the number of dimensions of an array. For example, 1-D array returns 1, a 2-D array returns 2, and so on.
Syntax:
CSharp
CSharp
public int Rank { get; }Property Value: It returns the rank (number of dimensions) of the Array of type System.Int32. Below programs illustrate the use of above-discussed property: Example 1:
// C# program to illustrate the
// Array.Rank Property
using System;
namespace geeksforgeeks {
class GFG {
// Main Method
public static void Main()
{
// declares a 1D Array of string.
string[] weekDays;
// allocating memory for days.
weekDays = new string[] {"Sun", "Mon", "Tue", "Wed",
"Thu", "Fri", "Sat" };
// using Rank Property
Console.WriteLine("Dimension of weekDays array: "
+ weekDays.Rank);
}
}
}
Output:
Example 2:
Dimension of weekDays array: 1
// C# program to illustrate the
// Array.Rank Property
using System;
namespace geeksforgeeks {
class GFG {
// Main Method
public static void Main()
{
// declaring an 2-D array
int[, ] arr2d = new int[4, 2];
// declaring an 3-D array
int[,, ] arr3d = new int[4, 2, 3];
// declaring an jagged array
int[][] jdarr = new int[2][];
// using Rank Property
Console.WriteLine("Dimension of arr2d array: "
+ arr2d.Rank);
Console.WriteLine("Dimension of arr3d array: "
+ arr3d.Rank);
// for the jagged array it
// will always return 1
Console.WriteLine("Dimension of jdarr array: "
+ jdarr.Rank);
}
}
}
Output:
Note:
Dimension of arr2d array: 2 Dimension of arr3d array: 3 Dimension of jdarr array: 1
- A jagged array (an array of arrays) is a one-dimensional array so the value of its Rank property is 1.
- Retrieving the value of this property is an O(1) operation.