The GetUpperBound () and GetLowerBound () function for an array

Can anyone tell what the two functions do? They take an integer argument, which is considered a dimension. But how does the meaning of this whole change the result?

Below is an example that I followed.

int[, ,] intMyArr = {{{ 7, 1, 3, 4 }, { 2, 9, 6, 5 } }, { { 7, 1, 3, 4 }, { 2, 9, 6, 5 }}}; Console.WriteLine(intMyArr.GetUpperBound(0)); // Output is 1 Console.WriteLine(intMyArr.GetUpperBound(1)); // Output is 1 Console.WriteLine(intMyArr.GetUpperBound(2)); // Output is 3 Console.WriteLine(intMyArr.GetLowerBound(0)); // Output is 0 Console.WriteLine(intMyArr.GetLowerBound(1)); // Output is 0 Console.WriteLine(intMyArr.GetLowerBound(2)); // Output is 0 

Any idea why GetLowerBound () always returns 0? If this always returns 0, then why do we need to call this method?

+6
source share
3 answers

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]; ... } 
+15
source

The Integer parameter GetUpper/LowerBound() indicates the dimension.

Some examples:

 // One-dimensional array var oneD = new object[5]; Console.WriteLine("Dimension 0 Lower bound: {0}", oneD.GetLowerBound(0)) // Outputs "Dimension 0 Lower bound: 0" Console.WriteLine("Dimension 0 Upper bound: {0}", oneD.GetUpperBound(0)) // Outputs "Dimension 0 Upper bound: 4" // Two-dimensional array var twoD = new object[5,10]; Console.WriteLine("Dimension 0 Lower bound: {0}", twoD.GetLowerBound(0)) // Outputs "Lower bound: 0" Console.WriteLine("Dimension 0 Upper bound: {0}", twoD.GetUpperBound(0)) // Outputs "Upper bound: 4" Console.WriteLine("Dimension 1 Lower bound: {0}", twoD.GetLowerBound(1)) // Outputs "Lower bound: 0" Console.WriteLine("Dimension 1 Upper bound: {0}", twoD.GetUpperBound(1)) // Outputs "Upper bound: 9" 

While arrays defined in C # have a lower bound = 0 and an upper bound = length - 1, arrays from other sources (for example, COM interaction) can have different borders, so those who work with the Excel interface, for example will be familiar with arrays that have a lower bound = 1, an upper bound = length.

+4
source

Can anyone tell what the two functions do?

This is written on their MSDN pages. They get the index of the first / last element of the specified dimension in the array. Take a look at Array.GetUpperBound and Array.GetLowerBound

They take an integer argument, which is considered a dimension.

Yes, as mentioned in Patashu, arrays can have multidimension .

Any idea why GetLowerBound () always returns 0? If this always returns 0, then why do we need to call this method?

In an array, each dimension can have its own specific lower and upper bounds. Thus, these methods can produce different results for each dimension of the array.

Note that although most arrays in the .NET Framework are zero-based ( that is, the GetLowerBound method returns zero for each array size ), the .NET Framework supports arrays that are not zero-based. Such arrays can be created using CreateInstance(Type, Int32\[\], Int32\[\]) , and can also be returned from unmanaged code.

Departure;

+2
source

Source: https://habr.com/ru/post/948291/


All Articles