ภาพรวม
เพื่อดึงขนาดของอาร์เรย์ 3 มิติใน C# ให้ใช้เมธอด GetLength(dimensionIndex) โดยระบุดัชนีมิติ (0, 1, 2)
รูปแบบการใช้งาน
int[,,] arr = new int[3, 4, 5]; int dim0 = arr.GetLength(0); // 3 int dim1 = arr.GetLength(1); // 4 int dim2 = arr.GetLength(2); // 5 int total = arr.Length; // 60 (3*4*5)
ตัวอย่างโค้ดเต็ม
using System;
class Program
{
static void Main()
{
int[,,] arr = new int[3, 4, 5];
Console.WriteLine(arr.GetLength(0)); // 3
Console.WriteLine(arr.GetLength(1)); // 4
Console.WriteLine(arr.GetLength(2)); // 5
Console.WriteLine(arr.Length); // 60
}
}
ผลลัพธ์
3 4 5 60
สรุปคุณสมบัติอาร์เรย์หลายมิติ
| คุณสมบัติ | คำอธิบาย |
|---|---|
GetLength(d) | ขนาดของมิติที่ d |
Length | จำนวนองค์ประกอบทั้งหมด (คูณทุกมิติ) |
Rank | จำนวนมิติ (3 สำหรับอาร์เรย์ 3 มิติ) |
GetUpperBound(d) | ดัชนีสูงสุดของมิติ d |
GetLowerBound(d) | ดัชนีต่ำสุดของมิติ d (มักเป็น 0) |