本文整理匯總了C#中System.Array.GetLength方法的典型用法代碼示例。如果您正苦於以下問題:C# Array.GetLength方法的具體用法?C# Array.GetLength怎麽用?C# Array.GetLength使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Array
的用法示例。
在下文中一共展示了Array.GetLength方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Main
//引入命名空間
using System;
public class SamplesArray
{
public static void Main()
{
// make a single dimension array
Array MyArray1 = Array.CreateInstance(typeof(int), 5);
// make a 3 dimensional array
Array MyArray2 = Array.CreateInstance(typeof(int), 5, 3, 2);
// make an array container
Array BossArray = Array.CreateInstance(typeof(Array), 2);
BossArray.SetValue(MyArray1, 0);
BossArray.SetValue(MyArray2, 1);
int i = 0, j, rank;
foreach (Array anArray in BossArray)
{
rank = anArray.Rank;
if (rank > 1)
{
Console.WriteLine("Lengths of {0:d} dimension array[{1:d}]", rank, i);
// show the lengths of each dimension
for (j = 0; j < rank; j++)
{
Console.WriteLine(" Length of dimension({0:d}) = {1:d}", j, anArray.GetLength(j));
}
}
else
{
Console.WriteLine("Lengths of single dimension array[{0:d}]", i);
}
// show the total length of the entire array or all dimensions
Console.WriteLine(" Total length of the array = {0:d}", anArray.Length);
Console.WriteLine();
i++;
}
}
}
輸出:
Lengths of single dimension array[0] Total length of the array = 5 Lengths of 3 dimension array[1] Length of dimension(0) = 5 Length of dimension(1) = 3 Length of dimension(2) = 2 Total length of the array = 30
示例2: Array.GetLength()
//引入命名空間
using System;
class MainClass
{
public static void Main()
{
string[,] names = {
{"J", "M", "P"},
{"S", "E", "S"},
{"C", "A", "W"},
{"G", "P", "J"},
};
int numberOfRows = names.GetLength(0);
int numberOfColumns = names.GetLength(1);
Console.WriteLine("Number of rows = " + numberOfRows);
Console.WriteLine("Number of columns = " + numberOfColumns);
}
}