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
廣告