Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

Array.LongLength Property ใน C#

Property Array.LongLength คืนค่าจำนวน Elements ทั้งหมดใน Array ทุก Dimension เป็น **64-bit Integer (long)** ใช้เมื่อ Array มีขนาดใหญ่เกิน int.MaxValue (2,147,483,647 Elements)

Syntax

long length = array.LongLength;

ความแตกต่าง Length vs LongLength

Property Return Type Max Value ใช้เมื่อ
Length int (32-bit) 2,147,483,647 Array ปกติ (Large Arrays รองรับใน .NET 4.5+)
LongLength long (64-bit) 9,223,372,036,854,775,807 Huge Arrays (> 2B Elements), 64-bit Indexing

ตัวอย่างโค้ด

using System;

class Program {
    static void Main() {
        // 2D Array: 15 x 35 = 525 Elements
        long[,] arr1 = new long[15, 35];
        
        // GetLength(0) = จำนวน Rows (Dimension 0)
        long len1 = arr1.GetLength(0);
        Console.WriteLine(len1);  // 15
        
        // LongLength = Total Elements ทุก Dimension
        Console.WriteLine(arr1.LongLength);  // 525
        
        // สำหรับ 1D Array - Length และ LongLength เท่ากัน
        int[] arr2 = new int[100];
        Console.WriteLine(arr2.Length);      // 100
        Console.WriteLine(arr2.LongLength);  // 100
    }
}

ผลลัพธ์

15
525

เมื่อไหร่ต้องใช้ LongLength?

// .NET 4.5+ รองรับ Large Arrays (> 2GB) ใน 64-bit Process
// ต้อง Enable ใน App.config หรือ .csproj:
/*

  
    
  

*/

// สร้าง Array ใหญ่กว่า int.MaxValue
// ต้องใช้ 64-bit Process และ gcAllowVeryLargeObjects
var hugeArray = new byte[3_000_000_000];  // 3 Billion Elements (~3 GB)

// Length จะ Overflow/Throw Exception (ในบาง Version)
int len = hugeArray.Length;  // อาจ Error หรือค่าผิด

// LongLength ทำงานถูกต้องเสมอ
long longLen = hugeArray.LongLength;  // 3,000,000,000

GetLongLength Method

สำหรับ Multi-dimensional Arrays ใช้ GetLongLength(dimension) เพื่อหาขนาดแต่ละ Dimension เป็น long

long[,] bigMatrix = new long[100000, 100000];  // 10 Billion Elements (ถ้ารองรับ)

long rows = bigMatrix.GetLongLength(0);   // 100000
long cols = bigMatrix.GetLongLength(1);   // 100000
long total = bigMatrix.LongLength;        // 10,000,000,000

ตัวอย่าง: Iterate Large Array ด้วย Long Index

// สำหรับ Array ใหญ่ที่ต้องใช้ long Index
// (Note: C# Array Indexer ใช้ int เท่านั้น ต้องใช้ Pointer หรือ Span)

unsafe {
    long[] largeArray = new long[1000];  // Example ขนาดเล็ก
    
    fixed (long* ptr = largeArray) {
        for (long i = 0; i < largeArray.LongLength; i++) {
            ptr[i] = i * i;
        }
    }
}

// หรือใช้ Span (Safe, Modern)
var largeArray = new long[1000];
Span span = largeArray;
for (long i = 0; i < span.Length; i++) {  // Span.Length เป็น int
    span[(int)i] = i * i;
}

Related Properties/Methods

Member Return Type คำอธิบาย
Length int Total Elements (32-bit)
LongLength long Total Elements (64-bit)
GetLength(int) int Size of Specific Dimension
GetLongLength(int) long Size of Specific Dimension (64-bit)
Rank int Number of Dimensions

Best Practices

  • ใช้ Length สำหรับ Array ทั่วไป (เร็วกว่า, รองรับใน .NET 4.5+ แล้ว)
  • ใช้ LongLength เมื่อทำงานกับ **Large Arrays** (> 2B Elements) หรือต้องการ 64-bit Arithmetic
  • สำหรับ Multi-dim Arrays ใช้ GetLength(d) / GetLongLength(d) เพื่อหาขนาดแต่ละมิติ
  • ใน 64-bit Process พร้อม gcAllowVeryLargeObjects Array ใหญ่กว่า 2GB สร้างได้