How to find the rank of an array in C# (original) (raw)

Last Updated : 23 Jan, 2019

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:

CSharp `

// 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:

Dimension of weekDays array: 1

Example 2:

CSharp `

// 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:

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

Note:

Reference:

Similar Reads

Introduction






Fundamentals







Control Statements





OOP Concepts






Methods






Arrays






ArrayList




String






Tuple





Indexers