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

หาจำนวน Elements ใน Array มิติที่กำหนดใน C#

Array Methods สำหรับ Dimension Info

Methodคืนค่าคำอธิบาย
GetLength(int dimension)intจำนวน Elements ใน Dimension ที่ระบุ (0-based)
GetLongLength(int dimension)longเหมือน GetLength แต่สำหรับ Array ใหญ่ (> 2^31)
GetLowerBound(int dimension)intLower Bound ของ Dimension (ปกติเป็น 0)
GetUpperBound(int dimension)intUpper Bound ของ Dimension
Rankintจำนวน Dimensions (Rank)
LengthintTotal Elements ทุก Dimension

1-D Array Example


using System;

public class Demo
{
    public static void Main()
    {
        string[] products = { "Andy", "Mark", "Gary", "Andre" };
        
        Console.WriteLine($"Rank (Dimensions): {products.Rank}");           // 1
        Console.WriteLine($"Length (Total): {products.Length}");           // 4
        Console.WriteLine($"GetLength(0): {products.GetLength(0)}");       // 4
        Console.WriteLine($"GetLongLength(0): {products.GetLongLength(0)}"); // 4
        Console.WriteLine($"GetLowerBound(0): {products.GetLowerBound(0)}"); // 0
        Console.WriteLine($"GetUpperBound(0): {products.GetUpperBound(0)}"); // 3
        
        // Properties
        Console.WriteLine($"IsFixedSize: {products.IsFixedSize}");         // True
        Console.WriteLine($"IsReadOnly: {products.IsReadOnly}");           // False
        Console.WriteLine($"IsSynchronized: {products.IsSynchronized}");   // False
    }
}

Output

Rank (Dimensions): 1
Length (Total): 4
GetLength(0): 4
GetLongLength(0): 4
GetLowerBound(0): 0
GetUpperBound(0): 3
IsFixedSize: True
IsReadOnly: False
IsSynchronized: False

Multi-Dimensional Array (Rectangular)


string[,] matrix = new string[3, 4]  // 3 Rows, 4 Columns
{
    { "A1", "A2", "A3", "A4" },
    { "B1", "B2", "B3", "B4" },
    { "C1", "C2", "C3", "C4" }
};

Console.WriteLine($"Rank: {matrix.Rank}");              // 2
Console.WriteLine($"Length: {matrix.Length}");          // 12 (Total)
Console.WriteLine($"GetLength(0): {matrix.GetLength(0)}"); // 3 (Rows)
Console.WriteLine($"GetLength(1): {matrix.GetLength(1)}"); // 4 (Cols)
Console.WriteLine($"GetLowerBound(0): {matrix.GetLowerBound(0)}"); // 0
Console.WriteLine($"GetUpperBound(0): {matrix.GetUpperBound(0)}"); // 2
Console.WriteLine($"GetLowerBound(1): {matrix.GetLowerBound(1)}"); // 0
Console.WriteLine($"GetUpperBound(1): {matrix.GetUpperBound(1)}"); // 3

Jagged Array (Array of Arrays)


string[][] jagged = new string[3][];
jagged[0] = new string[] { "A1", "A2" };
jagged[1] = new string[] { "B1", "B2", "B3" };
jagged[2] = new string[] { "C1" };

Console.WriteLine($"Rank: {jagged.Rank}");           // 1 (Jagged = 1-D Array of Arrays)
Console.WriteLine($"Length: {jagged.Length}");       // 3 (Outer Array Length)
Console.WriteLine($"jagged[0].Length: {jagged[0].Length}"); // 2
Console.WriteLine($"jagged[1].Length: {jagged[1].Length}"); // 3
Console.WriteLine($"jagged[2].Length: {jagged[2].Length}"); // 1

3-D Array Example


int[,,] cube = new int[2, 3, 4]; // 2x3x4 = 24 elements

Console.WriteLine($"Rank: {cube.Rank}");           // 3
Console.WriteLine($"Length: {cube.Length}");       // 24
Console.WriteLine($"Dim 0: {cube.GetLength(0)}");  // 2
Console.WriteLine($"Dim 1: {cube.GetLength(1)}");  // 3
Console.WriteLine($"Dim 2: {cube.GetLength(2)}");  // 4

// Iterate 3D
for (int i = 0; i < cube.GetLength(0); i++)
    for (int j = 0; j < cube.GetLength(1); j++)
        for (int k = 0; k < cube.GetLength(2); k++)
            cube[i, j, k] = i * 100 + j * 10 + k;

Helper Method: Print Array Info


public static void PrintArrayInfo(Array arr)
{
    Console.WriteLine($"Type: {arr.GetType().Name}");
    Console.WriteLine($"Rank: {arr.Rank}");
    Console.WriteLine($"Total Length: {arr.Length}");
    
    for (int d = 0; d < arr.Rank; d++)
    {
        Console.WriteLine($"Dimension {d}: Lower={arr.GetLowerBound(d)}, Upper={arr.GetUpperBound(d)}, Length={arr.GetLength(d)}");
    }
}

Best Practice

  • ใช้ GetLength(d) แทน Hardcode Magic Numbers
  • ใช้ GetLongLength สำหรับ Array ใหญ่ (> 2B elements)
  • Jagged Array (`[][]`) เร็วกว่า Multi-Dim (`[,]`) สำหรับ Row-major Access
  • Multi-Dim Array ใช้ Memory Contiguous (Cache Friendly สำหรับบาง Pattern)