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:
// 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 initializing 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);
}
}
}
Output:
ArrayRank is: 6
Example 2:
// 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 initializing 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);
}
}
}
Output:
The current type is not an Array Exception Thrown: System.ArgumentException
Reference: