Related Articles

Related Articles

C# | Type.GetArrayRank() Method
  • Last Updated : 30 Apr, 2019

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
Recommended Articles
Page :