本文整理匯總了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);
}
}