ขั้นแรก ตั้งค่าอาร์เรย์ −
int[] arr = {6, 3, 8, 4}; ตอนนี้ ใช้คุณสมบัติ Length เพื่อรับขนาดของอาร์เรย์ -
arr.Length
ให้เราดูรหัสที่สมบูรณ์ -
ตัวอย่าง
using System;
namespace Demo {
public class Demo {
public static void Main(string[] args) {
int[] arr = {6, 3, 8, 4};
Console.WriteLine("Array...");
foreach (int i in arr) {
Console.Write(i + " ");
}
Console.WriteLine("\nSize of Array: "+arr.Length);
}
}
} ผลลัพธ์
Array... 6 3 8 4 Size of Array: 4