C# 中陣列類的 Array.Length 屬性的作用是什麼?


C# 中的 Array.Lenth 屬性用於獲取陣列的長度。

我們首先設定該陣列類 −

Array arr = Array.CreateInstance(typeof(String), 3);
arr.SetValue("Electronics", 0);
arr.SetValue("Clothing", 1);
arr.SetValue("Appliances", 2);

由於陣列的長度為 3,因此 Length 屬性將生成結果 3 −

arr.Length

以下是實現陣列類的 Array.Length 屬性的程式碼 −

示例

 實際演示

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace lower {
   class Program {
      static void Main(string[] args) {
         Array arr = Array.CreateInstance(typeof(String), 3);
         arr.SetValue("Electronics", 0);
         arr.SetValue("Clothing", 1);
         arr.SetValue("Appliances", 2);

         Console.WriteLine("Length: {0}",arr.Length.ToString());

         Console.ReadLine();
      }
   }
}

輸出

Length: 3

更新於: 2020 年 6 月 20 日

69 次瀏覽

開啟你的 職業生涯

透過完成該課程獲得認證

開始學習
廣告