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

Three-Dimensional Array: int[3,4,5] Dimension 0 Length: 3 Rows Dimension 1 Length: 4 Columns Dimension 2 Length: 5 Depth Array Declaration: int[,,] arr = new int[3, 4, 5]; Total elements: 3 × 4 × 5 = 60

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.

Updated on: 2026-03-17T07:04:35+05:30

370 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements