Get bounds of a C# three-dimensional array

To get the bounds of a three-dimensional array in C#, use the GetUpperBound() and GetLowerBound() methods. These methods return the highest and lowest indices for a specified dimension of the array.

The parameter passed to these methods specifies the dimension (0, 1, or 2 for a three-dimensional array). Understanding array bounds is crucial for safe array traversal and avoiding index out-of-range exceptions.

Syntax

Following is the syntax for getting array bounds −

array.GetUpperBound(dimension)
array.GetLowerBound(dimension)

Parameters

  • dimension: An integer specifying the dimension of the array (0-based indexing).

Return Value

  • GetUpperBound() returns the highest index for the specified dimension.

  • GetLowerBound() returns the lowest index for the specified dimension (typically 0 in C#).

Three-Dimensional Array: int[3,4,5] Dimension 0 Length: 3 Lower: 0 Upper: 2 Dimension 1 Length: 4 Lower: 0 Upper: 3 Dimension 2 Length: 5 Lower: 0 Upper: 4 Upper bound = Array length - 1 Lower bound is typically 0 in C#

Example

The following example demonstrates how to get bounds for each dimension of a three-dimensional array −

using System;

class Program {
    static void Main() {
        int[,,] arr = new int[3,4,5];
        
        Console.WriteLine("Dimension 0 Upper Bound: {0}", arr.GetUpperBound(0));
        Console.WriteLine("Dimension 0 Lower Bound: {0}", arr.GetLowerBound(0));
        Console.WriteLine("Dimension 1 Upper Bound: {0}", arr.GetUpperBound(1));
        Console.WriteLine("Dimension 1 Lower Bound: {0}", arr.GetLowerBound(1));
        Console.WriteLine("Dimension 2 Upper Bound: {0}", arr.GetUpperBound(2));
        Console.WriteLine("Dimension 2 Lower Bound: {0}", arr.GetLowerBound(2));
        
        Console.WriteLine("\nArray dimensions: {0}", arr.Rank);
    }
}

The output of the above code is −

Dimension 0 Upper Bound: 2
Dimension 0 Lower Bound: 0
Dimension 1 Upper Bound: 3
Dimension 1 Lower Bound: 0
Dimension 2 Upper Bound: 4
Dimension 2 Lower Bound: 0

Array dimensions: 3

Using Bounds for Safe Array Traversal

This example shows how to use array bounds to safely iterate through a three-dimensional array −

using System;

class Program {
    static void Main() {
        int[,,] arr = new int[2,3,2];
        int counter = 1;
        
        // Fill the array
        for(int i = arr.GetLowerBound(0); i <= arr.GetUpperBound(0); i++) {
            for(int j = arr.GetLowerBound(1); j <= arr.GetUpperBound(1); j++) {
                for(int k = arr.GetLowerBound(2); k <= arr.GetUpperBound(2); k++) {
                    arr[i,j,k] = counter++;
                }
            }
        }
        
        // Display the array
        Console.WriteLine("Array contents:");
        for(int i = arr.GetLowerBound(0); i <= arr.GetUpperBound(0); i++) {
            for(int j = arr.GetLowerBound(1); j <= arr.GetUpperBound(1); j++) {
                for(int k = arr.GetLowerBound(2); k <= arr.GetUpperBound(2); k++) {
                    Console.WriteLine("arr[{0},{1},{2}] = {3}", i, j, k, arr[i,j,k]);
                }
            }
        }
    }
}

The output of the above code is −

Array contents:
arr[0,0,0] = 1
arr[0,0,1] = 2
arr[0,1,0] = 3
arr[0,1,1] = 4
arr[0,2,0] = 5
arr[0,2,1] = 6
arr[1,0,0] = 7
arr[1,0,1] = 8
arr[1,1,0] = 9
arr[1,1,1] = 10
arr[1,2,0] = 11
arr[1,2,1] = 12

Conclusion

The GetUpperBound() and GetLowerBound() methods provide safe access to array dimension limits. Use these methods when iterating through multi-dimensional arrays to avoid index out-of-range exceptions and ensure your code works with arrays of varying sizes.

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

553 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements