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


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

更新於:20-Jun-2020

69 次瀏覽

開始你的 職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.