## Difference between array.GetLength(0) and array.GetUpperBound(0)

c# array length

getlowerbound c#

c# multi-dimensional array length

c# array rank

c# 2d array

c# array of arrays

how to get row and column length of 2d array in c#

What is the difference between these two methods and when would you use one instead of the other?

int[,] array = new int[4,3]; int length0 = array.GetLength(0); int upperbound0 = array.GetUpperBound(0);

MSDN says that GetLength return the number of elements where as GetUpperBound determine the max index, but how could this be different since arrays are initialized with elements for each index?

Take a look at this (rarely used) method. From Docs:

`public static Array CreateInstance(Type elementType, int[] lengths, int[] lowerBounds)`

Creates a multidimensional Array of the specified Type and dimension lengths, with the specified lower bounds.

With it, you can create an array with indices from `-5 ... +5`

. If you ever use this kind of array, then `GetUpperBound()`

suddenly becomes a lot more useful than `GetLength()-1`

. There also exists a `GetLowerBound()`

.

But the C# support for this kind of arrays is low, you cannot use `[]`

. You would only need those methods in combination with the Array.GetValue() and SetValue() methods.

**Re: Array.getLength() vs Array.getUpperBound(),** What is the difference between these two methods and when would you use one instead of the other? int[,] array = new int[4,3]; int length0 = array.GetLength(0) 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. Applies to

`Array.Length`

returns the length of the array (number of elements) you need
to subtract 1 from it to get the UpperBound.

`Array.GetUpperBound(0)`

returns the upper bound of the array, you can use it
as is.

**Array.GetUpperBound(Int32) Method (System),** dimension: Int32. A zero-based dimension of the array whose upper bound needs to be determined. GetUpperBound(0); outer++) for (int inner = integers2d. Array.Length returns the length of the array (number of elements) you need. to subtract 1 from it to get the UpperBound. Array.GetUpperBound (0) returns the upper bound of the array, you can use it. as is I normally use Array.Length, as other collections have either a Length or.

`GetUpperBound`

returns the highest index in the array, the `GetLength`

returns the number of elements of the array.

i.e. GetUpperBound = GetLength - 1

**Array.Length vs Array.GetUpperBound(0) -- any real differences ,** Are there any differences between using Array.Length and. Array.GetUpperBound(0) on a one-dimensional array? We have a team of c# - Difference between array.GetLength(0) and array.GetUpperBound(0) What is the difference between these two methods and when would you use one instead of the other? int[,] array=new int[4,3]; int length0=array.GetLength(0); int upperbound0=array.GetUpperBound(0…

Generally, `array.GetUpperBound(0) = array.Length - 1`

, but since we can create arrays that have a Nonzero lower bound, that is not always true.

**Accelerated C# 2010,** All usage of these arrays boils down to method calls on a CLR-generated reference GetLength(0); ++i ) { for( int j = 0; j != GetUpperBound(1); ++j ) { Console. While GetLength () is a pre-define method of Array class. It has an argument which specify the dimension. If we pass 0 to GetLenth () method then it returns the size of first dimension. And if we pass 1 to GetLegnth () method then it returns the size of second dimension.

I realise this is an old question but I think it's worth emphasising that GetUpperBound returns the upper boundary of the specified dimension. This is important for a multidimensional array as in that case the two functions are not equivalent.

// Given a simple two dimensional array private static readonly int[,] USHolidays = { { 1, 1 }, { 7, 4 }, { 12, 24 }, { 12, 25 } };

The Length property will output 8 as there are 8 elements in the array.

Console.WriteLine(USHolidays.Length);

However, the GetUpperBound() function will output 3 as the upper boundary of the first dimension is 3. In other words I can loop over array indexes 0, 1, 2 and 3.

Console.WriteLine(USHolidays.GetUpperBound(0)); for (var i = 0; i <= USHolidays.GetUpperBound(0); i++) { Console.WriteLine("{0}, {1}", USHolidays[i, 0], USHolidays[i, 1]); }

**Accelerated VB 2005,** Length property returns the total number of elements in the array. In order to get the count GetUpperBound(0) For j As Integer = twoDim.GetLowerBound(1) To 2. The .Length property returns the number of elements in an array, whether it be one dimensional or multidimensional. That is a 2x6 array will have length of 12. The .GetLength (0) method returns number of elements in the row direction in a multidimensional array. For a 2x6 array that is 2.

**Illustrated C# 2012,** Array. From that base class they inherit a number of useful properties and the dimensions of the array GetLength Method Instance Returns the length of a particular dimension of the array Clear Method Static Sets a range of elements to 0 or null order of the elements of a range of a onedimensional array GetUpperBound 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

**Illustrated C# 2010,** Array. From that base class they inherit a number of useful properties and the dimensions of the array GetLength Method Instance Returns the length of a particular dimension of the array Clear Method Static Sets a range of elements to 0 or null order of the elements of a range of a onedimensional array GetUpperBound 2. GetLength (i) returns the number of elements in the i th dimension. So for a two-dimensional array GetLength (0) returns the number of rows and GetLength (1) returns the number of columns. share.

**c#,** int length0 = array.GetLength(0); int upperbound0 = array.GetUpperBound(0); MSDN indique que GetLength renvoie le n Différence entre array.GetLength (0) et GetUpperBound(0)); for (var i = 0; i <= USHolidays.GetUpperBound(0) I've got to maintain some code written by someone else who is no longer with the company. I'm seeing several references to java.lang.reflect.Array.getLength(anArray). Its working, but I've never seen reflection being used to get an array length. Does anyone know the difference between: java.lang.reflect.Array.getLength(anArray) and anArray.length

##### Comments

- You assume the lower bound is always zero. It is not.
- "but please don't do that.". Why not, if you are sure about your array's boundaries?
- I would only use that when I'm
*not*sure about the boundaries. - Usually. But that's not why it's provided.
- +1 to your answer, did not know that you could start an array at an index other than 0!