Some examples may help you understand this topic.
We use GetUpperBound() to determine the upper bound of the array for a given dimension, for example:
int[,,] A = new int[7, 9, 11]; // Returns 6: 0th dimension has 7 items, and so upper bound is 7 - 1 = 6; int upper0 = A.GetUpperBound(0); // Returns 8: 0th dimension has 7 items, 1st - 9 and so upper bound is 9 - 1 = 8; int upper1 = A.GetUpperBound(1); // Returns 10: 0th dimension has 7 items, 1st - 9, 2nd - 11 and so upper bound is 11 - 1 = 10; int upper2 = A.GetUpperBound(2);
usually GetLowerBound() returns 0 , because by default arrays are based on zero, but in some rare cases this is not so:
// A is [17..21] array: 5 items starting from 17 Array A = Array.CreateInstance(typeof(int), new int[] { 5 }, new int[] { 17 }); // Returns 17 int lower = A.GetLowerBound(0); // Returns 21 int upper = A.GetUpperBound(0);
A typical loop using GetLowerBound and GetUpperBound is
int[] A = ... for(int i = A.GetLowerBound(0); i <= A.GetUpperBound(0); ++i) { int item = A[i]; ... } // ... or multidimension int[,,] A = ...; for (int i = A.GetLowerBound(0); i <= A.GetUpperBound(0); ++i) for (int j = A.GetLowerBound(1); j <= A.GetUpperBound(1); ++j) for (int k = A.GetLowerBound(2); k <= A.GetUpperBound(2); ++k) { int item = A[i, j, k]; ... }
source share