在 C# 中獲取陣列的長度
Minahil Noor
2024年2月16日

本文將介紹一種在 C# 中查詢陣列長度的方法。
使用 Array.Length
屬性在 C# 中查詢陣列的長度
在 C# 中,我們可以使用 Array.Length
屬性來找到陣列的長度。此屬性查詢陣列中存在的元素總數。使用此屬性的正確語法如下。
ArrayName.Length;
該屬性返回陣列的長度。
下面的程式顯示瞭如何使用 Array.Length
屬性來查詢陣列的長度。
using System;
namespace FindLength {
class Length {
public static void Main() {
string[] flowers;
flowers = new string[] { "Rose", "Jasmine", "Lili", "Hibiscus", "Tulip", "Sun Flower" };
foreach (string flower in flowers) Console.Write(flower + "\n");
Console.Write("\nThe Number of Elements in the Array is: ");
Console.Write(flowers.Length);
}
}
}
輸出:
Rose
Jasmine
Lili
Hibiscus
Tulip
Sun Flower
The Number of Elements in the Array is: 6