Property Array.Length คืนค่าจำนวน Elements ทั้งหมดใน Array เป็น **32-bit Signed Integer (int)** เป็น Property ที่ใช้บ่อยที่สุดสำหรับหาขนาด Array
Syntax
int length = array.Length;
ตัวอย่างโค้ด
using System;
namespace Lower {
class Program {
static void Main(string[] args) {
// สร้าง Array ผ่าน Array.CreateInstance
Array arr = Array.CreateInstance(typeof(string), 3);
arr.SetValue("Electronics", 0);
arr.SetValue("Clothing", 1);
arr.SetValue("Appliances", 2);
// Length Property
Console.WriteLine("Length: {0}", arr.Length.ToString()); // 3
Console.ReadLine();
}
}
}
ผลลัพธ์
Length: 3
รายละเอียดสำคัญ
- Total Elements: รวมทุก Dimension (ไม่ใช่แค่ Dimension แรก)
- 1D Array:
Length == GetLength(0) - 2D Array:
Length == GetLength(0) * GetLength(1) - Jagged Array:
Length= จำนวน Sub-arrays (Rank=1) - Read-only Property: ไม่สามารถ Set ได้
- O(1): คืนค่าทันที ไม่ Iterate
ตัวอย่าง: Multi-dimensional Arrays
// 1D Array
int[] arr1D = new int[10];
Console.WriteLine(arr1D.Length); // 10
// 2D Array (Rectangular)
int[,] arr2D = new int[3, 4]; // 3 rows × 4 cols
Console.WriteLine(arr2D.Length); // 12 (Total)
Console.WriteLine(arr2D.GetLength(0)); // 3 (Rows)
Console.WriteLine(arr2D.GetLength(1)); // 4 (Cols)
// 3D Array
int[,,] arr3D = new int[2, 3, 4];
Console.WriteLine(arr3D.Length); // 24 (2×3×4)
// Jagged Array (Array of Arrays)
int[][] jagged = new int[3][];
jagged[0] = new int[2];
jagged[1] = new int[3];
jagged[2] = new int[4];
Console.WriteLine(jagged.Length); // 3 (Number of Sub-arrays, NOT Total Elements!)
// Total Elements ใน Jagged Array
int total = 0;
foreach (var sub in jagged) total += sub.Length;
Console.WriteLine("Total Elements: " + total); // 9
Array.Length vs List.Count vs Enumerable.Count()
| Expression | Type | Time Complexity | Note |
|---|---|---|---|
array.Length |
Property | O(1) | Fastest, Compile-time Constant สำหรับ Fixed Arrays |
list.Count |
Property | O(1) | List<T> Property |
collection.Count |
Property | O(1) | ICollection<T> Interface |
enumerable.Count() |
Extension Method | O(1) if ICollection, else O(n) | LINQ - ตรวจสอบ Interface ก่อน Iterate |
การใช้งานร่วมกับ Loop
int[] numbers = { 10, 20, 30, 40, 50 };
// Standard for Loop
for (int i = 0; i < numbers.Length; i++) {
Console.WriteLine(numbers[i]);
}
// Reverse Loop
for (int i = numbers.Length - 1; i >= 0; i--) {
Console.WriteLine(numbers[i]);
}
// foreach (ไม่ต้องใช้ Length)
foreach (int num in numbers) {
Console.WriteLine(num);
}
Edge Cases
// Empty Array
int[] empty = new int[0];
Console.WriteLine(empty.Length); // 0
// Null Array
int[] nullArray = null;
// Console.WriteLine(nullArray.Length); // NullReferenceException!
// Safe Check
int len = nullArray?.Length ?? 0; // 0 (C# 6+ Null-conditional)
// Array.CreateInstance with 0 Length
Array zeroArray = Array.CreateInstance(typeof(int), 0);
Console.WriteLine(zeroArray.Length); // 0
Performance Tips
- Cache
Lengthใน Loop ถ้าไม่เปลี่ยนขนาด Array:for (int i = 0, len = arr.Length; i < len; i++) - JIT Compiler มัก Optimize
arr.Lengthให้อยู่ใน Register แล้ว - หลีกเลี่ยง
arr.Count()(LINQ) บน Array - ใช้arr.Lengthแทน - ใช้
Span<T>สำหรับ High-performance Scenarios