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
How do you find the number of dimensions of an array in C#?
To find the number of dimensions of an array in C#, use the Rank property. This property returns an integer representing the total number of dimensions in the array.
Syntax
Following is the syntax for getting the number of dimensions −
int dimensions = arr.Rank;
For getting the length of specific dimensions, use the GetLength() method −
int lengthOfDimension = arr.GetLength(dimensionIndex);
Using Rank Property
The Rank property works with single-dimensional, multi-dimensional, and jagged arrays −
using System;
class Program {
static void Main() {
// Single-dimensional array
int[] arr1D = new int[5];
Console.WriteLine("1D Array dimensions: " + arr1D.Rank);
// Two-dimensional array
int[,] arr2D = new int[3, 4];
Console.WriteLine("2D Array dimensions: " + arr2D.Rank);
// Three-dimensional array
int[,,] arr3D = new int[2, 3, 4];
Console.WriteLine("3D Array dimensions: " + arr3D.Rank);
// Jagged array
int[][] jaggedArr = new int[3][];
Console.WriteLine("Jagged Array dimensions: " + jaggedArr.Rank);
}
}
The output of the above code is −
1D Array dimensions: 1 2D Array dimensions: 2 3D Array dimensions: 3 Jagged Array dimensions: 1
Using GetLength() for Dimension Sizes
While Rank tells you how many dimensions an array has, GetLength() tells you the size of each dimension −
using System;
class Program {
static void Main() {
int[,] arr = new int[3, 4];
Console.WriteLine("Number of dimensions: " + arr.Rank);
Console.WriteLine("Length of dimension 0 (rows): " + arr.GetLength(0));
Console.WriteLine("Length of dimension 1 (columns): " + arr.GetLength(1));
Console.WriteLine("Total elements: " + arr.Length);
Console.WriteLine("Upper Bound of dimension 0: " + arr.GetUpperBound(0));
Console.WriteLine("Lower Bound of dimension 0: " + arr.GetLowerBound(0));
}
}
The output of the above code is −
Number of dimensions: 3 Length of dimension 0 (rows): 3 Length of dimension 1 (columns): 4 Total elements: 12 Upper Bound of dimension 0: 2 Lower Bound of dimension 0: 0
Practical Example with Dynamic Analysis
Here's how you can analyze any array dynamically to understand its structure −
using System;
class Program {
static void AnalyzeArray(Array arr) {
Console.WriteLine("Array Analysis:");
Console.WriteLine("Dimensions: " + arr.Rank);
for (int i = 0; i < arr.Rank; i++) {
Console.WriteLine("Dimension " + i + " length: " + arr.GetLength(i));
}
Console.WriteLine("Total elements: " + arr.Length);
Console.WriteLine();
}
static void Main() {
int[] singleDim = new int[5];
int[,] twoDim = new int[3, 4];
int[,,] threeDim = new int[2, 3, 2];
AnalyzeArray(singleDim);
AnalyzeArray(twoDim);
AnalyzeArray(threeDim);
}
}
The output of the above code is −
Array Analysis: Dimensions: 1 Dimension 0 length: 5 Total elements: 5 Array Analysis: Dimensions: 2 Dimension 0 length: 3 Dimension 1 length: 4 Total elements: 12 Array Analysis: Dimensions: 3 Dimension 0 length: 2 Dimension 1 length: 3 Dimension 2 length: 2 Total elements: 12
Conclusion
The Rank property provides the number of dimensions in any C# array, while GetLength() gives the size of each specific dimension. These properties are essential for working with multi-dimensional arrays and understanding their structure programmatically.
