C# Array.GetLength() 和 Array.Length 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2044591/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:57:26  来源:igfitidea点击:

What is the difference between Array.GetLength() and Array.Length?

c#arrays

提问by Novice Developer

How do you use the Array.GetLengthfunction in C#?

你如何使用Array.GetLengthC#中的函数?

What is the difference between the Lengthproperty and the GetLengthfunction?

Length属性和GetLength功能有什么区别?

采纳答案by Mehrdad Afshari

GetLengthtakes an integer that specifies the dimension of the array that you're querying and returns its length. Lengthproperty returns the total number of items in an array:

GetLength接受一个整数,指定您正在查询的数组的维度并返回其长度。Length属性返回数组中的项目总数:

int[,,] a = new int[10,11,12];
Console.WriteLine(a.Length);           // 1320
Console.WriteLine(a.GetLength(0));     // 10
Console.WriteLine(a.GetLength(1));     // 11
Console.WriteLine(a.GetLength(2));     // 12

回答by Henk Holterman

For 1-dimensional arrays Lengthand GetLength(0)are exactly the same.

对于一维数组LengthGetLength(0)是完全一样的。

For arrays of higher rank Lengthis the product of all GetLength(0..Rank-1)values, in other words it is always the total number of fields.

对于更高等级的数组,它是Length所有GetLength(0..Rank-1)值的乘积,换句话说,它始终是字段的总数。

回答by Jason Williams

GetLength returns the length of a specified dimension of a mulit-dimensional array.

GetLength 返回多维数组的指定维度的长度。

Length returns the sum of the total number of elements in all the dimensions.

长度返回所有维度中元素总数的总和。

  • For a single-dimensional array, Length == GetLength(0)
  • For a two-dimensional array, Length == GetLength(0) * GetLength(1)
  • 对于一维数组,Length == GetLength(0)
  • 对于二维数组,Length == GetLength(0) * GetLength(1)

etc.

等等。

回答by Vineetha Mathew

In mathematical term, we call as m rows and n columns, so the results is product of m*n for a two dimensional array. In this case GetLength(0) = mrows and GetLength(1)= ncolumns. For e.g see below example

在数学术语中,我们称为 m 行 n 列,因此对于二维数组,结果是 m*n 的乘积。在这种情况下, GetLength(0) = m行和 GetLength(1)= n列。例如,见下面的例子

string[,] stocks ={{"RELIND","Reliance Industries","1006.30"},{"TATMOB","Tata Mobiles","504.10"},{"ALST","Allstate","800.00"}, {"GE","GE Motors","810.00"}
};

The stocks arrays return GetLength(0)= 4and GetLength(1)=3and length =12

股票阵列回报GetLength(0)= 4GetLength(1)=3length =12

回答by K_Unit

The .Lengthproperty 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.

.Length属性返回数组中元素的数量,无论是一维还是多维。即 2x6 数组的长度为 12。

The .GetLength(0)method returns number of elements in the row direction in a multidimensional array. For a 2x6 array that is 2.

.GetLength(0)方法返回多维数组中行方向的元素数。对于为 2 的 2x6 数组。

The .GetLength(1)method returns number of elements in the column direction in a multidimensional array. For a 2x6 array that is 6.

.GetLength(1)方法返回多维数组中列方向的元素数。对于 6 的 2x6 阵列。

These do not return an actual element value, as stated by the chosen answer above.

正如上面选择的答案所述,这些不会返回实际的元素值。