How to find the rank of an array in C#

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:

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}
}

chevron_right


Output:

Dimension of weekDays array: 1

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}
}

chevron_right


Output:

Dimension of arr2d array: 2
Dimension of arr3d array: 3
Dimension of jdarr array: 1

Note:

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.