Get Upperbound and Lowerbound of a three-dimensional array in C#

CsharpProgrammingServer Side Programming

To get the Upperbound and Lowerbound, use the GetUpperBound() GetLowerBound() methods in C#, respectively.

The parameter to be set under these methods is the dimensions i.e.

Let’s say our 3D array is −

int[,,] arr = new int[2,3,4];

For a three-dimensional arrays, dimension 0.

arr.GetUpperBound(0)
arr.GetLowerBound(0)

For a three-dimensional arrays, dimension 1.

arr.GetUpperBound(1)
arr.GetLowerBound(1)

For a three-dimensional arrays, dimension 2.

arr.GetUpperBound(2)
arr.GetLowerBound(2)

Example

 Live Demo

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

Output

Dimension 0 Upper Bound: 1
Dimension 0 Lower Bound: 0
Dimension 1 Upper Bound: 2
Dimension 1 Lower Bound: 0
Dimension 2 Upper Bound: 3
Dimension 2 Lower Bound: 0
raja
Published on 14-Sep-2018 16:44:12
Advertisements