如何在 C# 中定義一個數組的秩?
要找到一個數組的維度數,請使用 Array Rank 屬性。你可以以下方式進行定義 -
arr.Rank
在此處,arr 是我們的陣列 -
int[,] arr = new int[3,4];
如果你想要獲取它的行和列,那麼請使用 GetLength 屬性 -
arr.GetLength(0); arr.GetLength(1);
以下為完整程式碼 -
示例
using System; class Program { static void Main() { int[,] arr = new int[3,4]; Console.WriteLine(arr.GetLength(0)); Console.WriteLine(arr.GetLength(1)); // Length Console.WriteLine(arr.Length); Console.WriteLine("Dimensions of Array : " + arr.Rank); } }
輸出
3 4 12 Dimensions of Array : 2
廣告