C# 数组的 GetUpperBound() 和 GetLowerBound() 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17358139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GetUpperBound() and GetLowerBound() function for array
提问by Narendra
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?
知道为什么 GetLowerBound() 总是返回 0 吗?如果这总是返回 0 那么我们为什么需要调用这个方法?
回答by Iridium
The integer parameter to GetUpper/LowerBound()specifies the dimension.
用于GetUpper/LowerBound()指定维度的整数参数。
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.
虽然在 C# 中定义的数组具有下限 = 0 和上限 = length - 1,但来自其他来源(例如 COM 互操作)的数组可以具有不同的范围,因此那些使用 Excel 互操作的人将熟悉具有下限的数组 = 1、上限=长度。
回答by Soner G?nül
Can anyone please tell what does the two functions do?
谁能告诉我这两个函数的作用是什么?
It is written in their MSDN pages. They gets the index of the first / lastelement of the specified dimension in the array. Take a look Array.GetUpperBoundand Array.GetLowerBound
它写在他们的 MSDN 页面中。它们获取数组中指定维度的第一个/最后一个元素的索引。看看Array.GetUpperBound和Array.GetLowerBound
They take an integer argument which is told to be dimension.
他们采用一个整数参数,该参数被称为维度。
Yes, as Patashu mentioned, arrays can have multidimension.
Any idea why GetLowerBound() is always returning 0? If this always returns 0 then why do we need to call this method?
知道为什么 GetLowerBound() 总是返回 0 吗?如果这总是返回 0 那么我们为什么需要调用这个方法?
In an array, every dimension can have their specific lower and uppor bounds. So, this methods can create different results for each dimension of array.
在数组中,每个维度都可以有其特定的下限和上限。因此,这种方法可以为数组的每个维度创建不同的结果。
Note that, although most arrays in the .NET Framework are zero-based (that is, the
GetLowerBoundmethod returns zero for each dimension of an array), the .NET Framework does support arrays that are not zero-based. Such arrays can be created with theCreateInstance(Type, Int32\[\], Int32\[\])method, and can also be returned from unmanaged code.
请注意,尽管 .NET Framework 中的大多数数组都是从零开始的(即该
GetLowerBound方法对数组的每个维度都返回零),但 .NET Framework 确实支持非从零开始的数组。此类数组可以使用CreateInstance(Type, Int32\[\], Int32\[\])方法创建, 也可以从非托管代码返回。
Check out;
查看;
回答by Dmitry Bychenko
May be some examples make the topic clear for you
可能是一些例子让你清楚这个主题
We use GetUpperBound()to find out the upper boundof an array for given dimension,
like that:
我们GetUpperBound()用来找出给定维度的数组的上限,如下所示:
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-basedby default,
but in some rare cases they are not:
通常,GetLowerBound()返回0,因为默认情况下数组是从零开始的,但在极少数情况下它们不是:
// 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 GetLowerBoundand GetUpperBoundis
使用GetLowerBoundand 的典型循环GetUpperBound是
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];
...
}

