array.GetLength(0) 和 array.GetUpperBound(0) 之间的区别

发布于 2024-12-01 07:42:35 字数 256 浏览 0 评论 0原文

这两种方法有什么区别?什么时候会使用其中一种而不是另一种?

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

MSDN 说 GetLength 返回元素的数量,而 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);
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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

如歌彻婉言 2024-12-08 07:42:35

看看这个(很少使用)方法。来自文档

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

创建指定类型和维度长度以及指定下限的多维数组。

使用它,您可以创建一个索引为 -5 ... +5 的数组。如果您曾经使用过这种数组,那么 GetUpperBound() 突然变得比 GetLength()-1 有用得多。还有一个GetLowerBound()

但是C#对这种数组的支持很低,不能使用[]。您只需要将这些方法与 Array.GetValue() 和 SetValue() 方法结合使用。

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.

听闻余生 2024-12-08 07:42:35

Array.Length 返回您需要的数组的长度(元素数量)
从中减去 1 即可得到 UpperBound。

Array.GetUpperBound(0) 返回数组的上界,可以使用它
照原样。

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.

遮云壑 2024-12-08 07:42:35

GetUpperBound 返回数组中的最高索引,GetLength 返回数组的元素数量。

即 GetUpperBound = GetLength - 1

GetUpperBound returns the highest index in the array, the GetLength returns the number of elements of the array.

i.e. GetUpperBound = GetLength - 1

屋檐 2024-12-08 07:42:35

一般来说,array.GetUpperBound(0) = array.Length - 1,但由于我们可以创建具有非零下界的数组,因此情况并不总是如此。

Generally, array.GetUpperBound(0) = array.Length - 1, but since we can create arrays that have a Nonzero lower bound, that is not always true.

冷血 2024-12-08 07:42:35

我意识到这是一个老问题,但我认为值得强调 GetUpperBound 返回上边界指定尺寸。这对于多维数组很重要,因为在这种情况下两个函数不相等。

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

由于数组中有 8 个元素,因此 Length 属性将输出 8。

Console.WriteLine(USHolidays.Length);

但是,GetUpperBound() 函数将输出 3,因为第一维的上边界是 3。换句话说,我可以循环数组索引 0、1、2 和 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]);
}

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]);
}
So尛奶瓶 2024-12-08 07:42:35

如果你的数组的下限是 0 那么你可以使用它们中的任何一个而不会产生任何混淆,但我会推荐 array.length-1 因为它被广泛使用。但是,如果数组的下限小于 0,那么您应该使用 array.GetUpperBound(0),因为在这种情况下 array.length-1 != array.getUpperBound(0)

if lower bound of your array is 0 then you can use either of them without any confusion but i would recommend array.length-1 as it is widely used. however, if the lower bound of your array is less than 0 then you should use array.GetUpperBound(0) because in this case array.length-1 != array.getUpperBound(0)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文