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
Get the width and height of a three-dimensional array
To get the width and height of a three-dimensional array in C#, we use the Array.GetLength() method. This method returns the number of elements in a specified dimension of the array.
In a three-dimensional array declared as int[,,] arr = new int[3,4,5], the dimensions represent different aspects of the array structure.
Syntax
Following is the syntax for getting dimensions of a three-dimensional array −
arrayName.GetLength(dimension)
Where dimension is −
0− First dimension (rows)1− Second dimension (columns)2− Third dimension (depth)
Understanding Three-Dimensional Array Dimensions
Example
Let's create a three-dimensional array and get the length of each dimension −
using System;
class Program {
static void Main() {
int[,,] arr = new int[3,4,5];
Console.WriteLine("Dimension 0 (Rows): " + arr.GetLength(0));
Console.WriteLine("Dimension 1 (Columns): " + arr.GetLength(1));
Console.WriteLine("Dimension 2 (Depth): " + arr.GetLength(2));
Console.WriteLine("Total elements: " + (arr.GetLength(0) * arr.GetLength(1) * arr.GetLength(2)));
}
}
The output of the above code is −
Dimension 0 (Rows): 3 Dimension 1 (Columns): 4 Dimension 2 (Depth): 5 Total elements: 60
Using GetLength() with Different Array Sizes
Example
using System;
class Program {
static void Main() {
// Different sized three-dimensional arrays
int[,,] smallArray = new int[2,3,4];
int[,,] largeArray = new int[10,15,20];
Console.WriteLine("Small Array Dimensions:");
Console.WriteLine("Rows: " + smallArray.GetLength(0));
Console.WriteLine("Columns: " + smallArray.GetLength(1));
Console.WriteLine("Depth: " + smallArray.GetLength(2));
Console.WriteLine("\nLarge Array Dimensions:");
Console.WriteLine("Rows: " + largeArray.GetLength(0));
Console.WriteLine("Columns: " + largeArray.GetLength(1));
Console.WriteLine("Depth: " + largeArray.GetLength(2));
}
}
The output of the above code is −
Small Array Dimensions: Rows: 2 Columns: 3 Depth: 4 Large Array Dimensions: Rows: 10 Columns: 15 Depth: 20
Conclusion
The GetLength() method is essential for determining the size of each dimension in a three-dimensional array. Use GetLength(0) for rows, GetLength(1) for columns, and GetLength(2) for depth to access the array dimensions programmatically.
