In this guide, we will explore the Type.GetArrayRank method in C# along with its syntax and illustrations.
What is the Type.GetArrayRank method?
The GetArrayRank method in C# belongs to the System namespace and is a reflection method. This particular method is a crucial feature of the Type class within the Common Language Runtime (CLR) environment. Its primary purpose is to ascertain the rank or dimensionality of an array.
Basics of C# Arrays
Arrays are fundamental data structures that store elements of identical kinds in contiguous memory locations. Arrays can be categorized as single-dimensional, multidimensional, or jagged. A single-dimensional array is akin to a vector, whereas a multidimensional array can encompass more than two dimensions.
Introduction to the Type Class
The Type class is a component of the .NET Framework that offers a sophisticated array of functions for retrieving type-specific information during program execution. This class is frequently employed in reflection, enabling dynamic inspection and interaction with types by the system.
The GetArrayRank method is tailored for array data types and is instrumental in determining the dimensionality or number of dimensions of a given array type. This function proves especially handy when working with arrays of diverse sizes, requiring the program to adapt its operations accordingly.
Syntax:
It has the following syntax:
public virtual int GetArrayRank().
The function provides an integer value that indicates the total number of dimensions within the current type.
This function will raise an ArgumentException if the current data type is not compatible with an array.
Example 1:
Let's consider an example to demonstrate the implementation of the Type.GetArrayRank method in the C# programming language.
using System;
using System.Globalization;
using System.Reflection;
class ArrayRank{
// Main Method
public static void Main()
{
try
{
// the object type declaration
Type objtype = typeof(int[,,,,,,,]);
// value to store the dimension using GetArrayRank() method
int rankValue = objtype.GetArrayRank();
// print statement
Console.WriteLine("The ArrayRank is: {0}", rankValue);
}
catch (ArgumentException ex)
{
Console.WriteLine("The type is not in the given array");
Console.Write("The Exception is Thrown: ");
Console.Write("{0}", ex.GetType(), ex.Message);
}
}
}
Output:
The ArrayRank is: 8
Example 2:
Let's consider another example to demonstrate the functionality of the Type.GetArrayRank method in the C# language.
using System;
using System.Globalization;
using System.Reflection;
class ArrayRank{
// Main Method
public static void Main()
{
try
{
// the object type declaration
Type objtype = typeof(int);
// value to store the dimension using GetArrayRank() method
int rankValue = objtype.GetArrayRank();
// print statement
Console.WriteLine("The ArrayRank is: {0}", rankValue);
}
catch (ArgumentException ex)
{
Console.WriteLine("The type is not in the given array");
Console.Write("The Exception is Thrown: ");
Console.Write("{0}", ex.GetType(), ex.Message);
}
}
}
Output:
The type is not in the given array
The Exception is Thrown: System.ArgumentException