Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Type.GetArrayRank() Method in C#
The Type.GetArrayRank() method in C# returns the number of dimensions in an array type. This method is useful when working with multidimensional arrays and you need to determine how many dimensions the array has at runtime.
The method works only with array types. If called on a non-array type, it throws an ArgumentException.
Syntax
Following is the syntax for the GetArrayRank() method −
public virtual int GetArrayRank();
Return Value
The method returns an int representing the number of dimensions in the array. For example, a single-dimensional array returns 1, a two-dimensional array returns 2, and so on.
Examples
Example 1: Three-Dimensional Array
This example demonstrates getting the rank of a three-dimensional array −
using System;
public class Demo {
public static void Main(string[] args) {
Type type = typeof(int[,,]);
int arrRank = type.GetArrayRank();
Console.WriteLine("Array Rank = {0}", arrRank);
}
}
The output of the above code is −
Array Rank = 3
Example 2: Eight-Dimensional Array with Type Comparison
This example shows an eight-dimensional array and compares two identical array types −
using System;
public class Demo {
public static void Main(string[] args) {
Type type = typeof(string[,,,,,,,]);
Type type2 = typeof(string[,,,,,,,]);
int arrRank = type.GetArrayRank();
Console.WriteLine("Array Rank = {0}", arrRank);
Console.WriteLine("Are both types equal? {0}", type.Equals(type2));
}
}
The output of the above code is −
Array Rank = 8 Are both types equal? True
Example 3: Different Array Dimensions
This example demonstrates the difference between single-dimensional and multidimensional arrays −
using System;
public class Demo {
public static void Main(string[] args) {
Type singleDim = typeof(int[]);
Type twoDim = typeof(int[,]);
Type fourDim = typeof(double[,,,]);
Console.WriteLine("Single-dimensional array rank: {0}", singleDim.GetArrayRank());
Console.WriteLine("Two-dimensional array rank: {0}", twoDim.GetArrayRank());
Console.WriteLine("Four-dimensional array rank: {0}", fourDim.GetArrayRank());
}
}
The output of the above code is −
Single-dimensional array rank: 1 Two-dimensional array rank: 2 Four-dimensional array rank: 4
Common Use Cases
-
Dynamic array processing: When working with arrays of unknown dimensions at compile time.
-
Reflection scenarios: When inspecting array types through reflection.
-
Generic array utilities: Building methods that work with arrays of any dimension.
Conclusion
The Type.GetArrayRank() method provides a simple way to determine the number of dimensions in an array type. It returns an integer representing the rank, from 1 for single-dimensional arrays to higher values for multidimensional arrays, making it essential for reflection-based array operations.
