如何在 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

更新日期: 2020 年 6 月 20 日

347 次瀏覽

啟動您的 職業生涯

完成課程即可獲得認證

開始學習
廣告