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:
csharp
using System;
using System.Globalization;
using System.Reflection;
class GFG {
public static void Main()
{
try
{
Type type = typeof ( int [,,,,, ]);
int rank = type.GetArrayRank();
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);
}
}
}
|
Example 2:
csharp
using System;
using System.Globalization;
using System.Reflection;
class GFG {
public static void Main()
{
try {
Type type = typeof ( int );
int rank = type.GetArrayRank();
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:
Last Updated :
11 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...