Can anyone please tell what does the two functions do? They take an integer argument which is told to be dimension. But how does the value of this integer changes the output?
Below is an example which I ran.
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() is always returning 0? If this always returns 0 then why do we need to call this method?
For upper_bound(): Initialise the startIndex as 0 and endIndex as N – 1. Compare K with the middle element(say arr[mid]) of the array. If the middle element is less than equals to K then update the startIndex as middle index + 1(mid + 1).
GetUpperBound() Method is used to find the index of the last element of the specified dimension in the array. Syntax: public int GetUpperBound (int dimension); Here, dimension is a zero-based dimension of the array whose upper bound needs to be determined.
The lower bound of array specifies the lowest index of the array and upper bound specifies the highest index of the array.
GetUpperBound(0) returns the last index in the first dimension of the array, and GetUpperBound(Rank - 1) returns the last index of the last dimension of the array. This method is an O(1) operation.
The integer parameter to GetUpper/LowerBound()
specifies 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"
Whilst arrays defined within C# have lower bound = 0 and upper bound = length - 1, arrays from other sources (e.g. COM interop) can have different bounds, so those working with Excel interop for example will be familiar with arrays that have lower bound = 1, upper bound = length.
May be some examples make the topic clear for you
We use GetUpperBound()
to find out the upper bound of an array for given dimension,
like that:
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, since arrays are zero-based by default,
but in some rare cases they are not:
// 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);
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];
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With