本文整理汇总了C#中System.Array.Length属性的典型用法代码示例。如果您正苦于以下问题:C# Array.Length属性的具体用法?C# Array.Length怎么用?C# Array.Length使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类System.Array
的用法示例。
在下文中一共展示了Array.Length属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
// Declare a single-dimensional string array
String[] array1d = { "zero", "one", "two", "three" };
ShowArrayInfo(array1d);
// Declare a two-dimensional string array
String[,] array2d = { { "zero", "0" }, { "one", "1" },
{ "two", "2" }, { "three", "3"},
{ "four", "4" }, { "five", "5" } };
ShowArrayInfo(array2d);
// Declare a three-dimensional integer array
int[, ,] array3d = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } },
{ { 7, 8, 9 }, { 10, 11, 12 } } };
ShowArrayInfo(array3d);
}
private static void ShowArrayInfo(Array arr)
{
Console.WriteLine("Length of Array: {0,3}", arr.Length);
Console.WriteLine("Number of Dimensions: {0,3}", arr.Rank);
// For multidimensional arrays, show number of elements in each dimension.
if (arr.Rank > 1) {
for (int dimension = 1; dimension <= arr.Rank; dimension++)
Console.WriteLine(" Dimension {0}: {1,3}", dimension,
arr.GetUpperBound(dimension - 1) + 1);
}
Console.WriteLine();
}
}
输出:
Length of Array: 4 Number of Dimensions: 1 Length of Array: 12 Number of Dimensions: 2 Dimension 1: 6 Dimension 2: 2 Length of Array: 12 Number of Dimensions: 3 Dimension 1: 2 Dimension 2: 2 Dimension 3: 3
示例2: Main
//引入命名空间
using System;
class MainClass {
public static void Main() {
int[,,] nums = new int[10, 5, 6];
Console.WriteLine("Length of nums is " + nums.Length);
}
}