Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of Array.GetLowerBound(int)?

Tags:

arrays

.net

I don't understand the purpose of Array.GetLowerBound().

Does it ever return non-zero? When? How?

Thanks.

like image 812
Cheeso Avatar asked Aug 19 '09 17:08

Cheeso


People also ask

What is Upperbound in C#?

The GetUpperBound() method of array class in C# gets the upper bound of the specified dimension in the Array. Firstly, set the array and get the upperbound as shown below − arr.GetUpperBound(0).ToString() The following is an example stating the usage of GetUpperBound() method in C#.

What is GetUpperBound in VB?

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.

What is upper bound and lower bound in array in C#?

The lower bound of array specifies the lowest index of the array and upper bound specifies the highest index of the array.

How do you find the upper bound of an array?

In order to get the array upperbound of a multidimensional array, we use the length() method. For a 2D array, the length() method returns the number of rows. We can access the number of columns using the array_name[0].


2 Answers

Theoretically, you can create arrays with any lower or upper bound for indexing. VB.NET can use this to make arrays with a lower bound of 1 in order to be compatible with some older VB verions, but you can actually use Array.CreateInstance(Type,Int32[],Int32[]) to create an array with any lower bound you wish.

like image 64
Reed Copsey Avatar answered Oct 01 '22 07:10

Reed Copsey


On a multi dimensional array in VB or various COM derived APIs you can query the lower bound by dimension. Array types can be 0 or 1 based (i.e. starting from zero or 1) and this applies to multidimensional arrays as well.

This can also apply to arrays exposed through COM interop. For example, many Excel APIs use 1-based arrays and many APIs functions use variant arrays as parameters (the variant was essentially invented as a data type for a spreadsheet cell).

When using COM interop you still have to play nicely with these APIs and type systems. They were originally designed to be used with VBA, and the 'classic' VB4-6 language variants had a truly baroque type system due to their tight coupling with COM. The .Net type systems of C# et. al. are somewhat less painful than their COM-based predecessors, but you still get to feel the pain when using COM interop.

like image 38
ConcernedOfTunbridgeWells Avatar answered Oct 01 '22 08:10

ConcernedOfTunbridgeWells