首页 > 其他分享 >Difference between array.GetLength(0) and array.GetUpperBound(0)

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

时间:2022-10-09 16:57:01浏览次数:64  
标签:GetUpperBound int GetLength between array Difference methods

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

 

标签:GetUpperBound,int,GetLength,between,array,Difference,methods
From: https://www.cnblogs.com/chucklu/p/16772748.html

相关文章

  • list、set、map以及array的区别
    对于刚刚学习集合框架来说,如何选择list、set、map以及array是比较模糊的在此我将对这四种情况做总结:array:数组,可以存储对象和基本数据类型,长度固定。Collection:集合(单......
  • ArrayList源码学习
    arraylist1、总体关系图  1.1:Serializable接口这是一个空接口,只有实现了这个接口的对象才可以进行序列化。然后这个序列化id是为了保证反序列化成功也就是在运......
  • 如何使用JavaScript将Set转换为Array?
    https://blog.csdn.net/m0_66319974/article/details/122436737在JavaScript中,想要将Set(集合)转换为Array数组,可以通过以下方式实现。方法1:使用Array.from()方法Array.......
  • 【Java基础】Collections集合概述和使用、ArrayList集合存储学生并排序及斗地主案例
    目录​​一、Collections概述和使用​​​​二、ArrayList集合存储学生并排序​​​​三、斗地主案例​​一、Collections概述和使用Collection类的作用:是针对集合操作的工......
  • Array Partition
    题意选出三个区间,区间不重叠,且完美覆盖序列a[n]左区间最大值等于中区间最小值等于右区间最大值可以发现,在左区间确定的情况下,中区间和右区间的断点是具有单调性的越往......
  • 【重识Java】一文弄懂ArrayList所有常见操作
    ArrayList类是一个可以动态修改的数组,与普通数组的区别就是它是没有固定大小的限制,我们可以添加或删除元素。ArrayList继承了AbstractList,并实现了List接口。一、基础......
  • 江苏工匠杯easyphp(array_search绕过)
    <?phphighlight_file(__FILE__);$key1=0;$key2=0;$a=$_GET['a'];$b=$_GET['b'];if(isset($a)&&intval($a)>6000000&&strlen($a)<=3){if(isse......
  • Educational Codeforces Round 100 (Rated for Div. 2) B. Find The Array(思维)
    https://codeforces.com/contest/1463/problem/B题目大意:给定n个数字的数组a,让我们凑出数组b;满足b[i]要么可以整除b[i+1],要么可以被b[i+1]整除,同时2*求和abs(a[i]-b[......
  • jira项目笔记17-自定义useArray
    2-1、要求自定义一个useArray的customhook。结合react-hook和typescript,实现对数组简单的增加、删除、清空的那个功能,并且对增加的对象类型有限制2-2、代码实现export......
  • Java中List和ArrayList的区别,为什么用接口来引用对象而不是类
    区别用几句话来简单概述就是:1、List是一个接口,而ArrayList是List接口的一个实现类。2、ArrayList类继承并实现了List接口。3、因此,List接口不能被构造,也就是我们说的不能......