C# | Type.GetArrayRank() Method

Type.GetArrayRank() Method is used to get the number of dimensions in an array.

Syntax: public virtual int GetArrayRank ();

Return Value: This method returns an integer which contains the number of dimensions in the current type.



Exception: This method throws ArgumentException if the current type is not an array.

Below programs illustrate the use of Type.GetArrayRank() Method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.GetArrayRank() Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        try 
        {
            // Declaring and intializing Type object
            Type type = typeof(int[,,,,, ]);
  
            // Getting the dimensions
            // using GetArrayRank()
            int rank = type.GetArrayRank();
  
            // Display the rank
            Console.WriteLine("ArrayRank is:  {0}", rank);
        }
  
        catch (ArgumentException e)
        {
            Console.WriteLine("The current type is not an Array");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

chevron_right


Output:

ArrayRank is:  6

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.GetArrayRank() Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        try {
  
            // Declaring and intializing Type object
            Type type = typeof(int);
  
            // Getting ArrayRank by
            // using GetArrayRank()
            int rank = type.GetArrayRank();
  
            // Display the rank
            Console.WriteLine("ArrayRank is :  {0}", rank);
        }
  
        catch (ArgumentException e) 
        {
            Console.WriteLine("The current type is not an Array");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

chevron_right


Output:

The current type is not an Array
Exception Thrown: System.ArgumentException

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.