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

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);

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?

 

回答1

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.

 

回答2

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]);
}

 

 

 

Array.GetUpperBound(Int32) Method

Definition

Gets the index of the last element of the specified dimension in the array.

C#
public int GetUpperBound (int dimension);

Parameters

dimension
Int32

A zero-based dimension of the array whose upper bound needs to be determined.

Returns

Int32

The index of the last element of the specified dimension in the array, or -1 if the specified dimension is empty.

Exceptions

dimension is less than zero.

-or-

dimension is equal to or greater than Rank.

 

Remarks

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.

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(39)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2021-10-09 If I do not specify [httpGet] or [httpPost] for the action method , what will be the rule for calling it
2019-10-09 Snoop
2018-10-09 asp.net cookie and session
点击右上角即可分享
微信分享提示