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

Multi-dimensional Array ที่เรียบง่ายที่สุดใน C#

Multi-dimensional Array ที่เรียบง่ายที่สุดใน C# คือ Two-dimensional Array (อาร์เรย์ 2 มิติ) คล้ายตารางที่มี x แถว และ y คอลัมน์

สามารถ Initialize ได้โดยระบุค่าในวงเล็บสำหรับแต่ละแถว ตัวอย่างอาร์เรย์ 4 แถว 4 คอลัมน์:

int[,] a = new int[4,4] {
    {0, 1, 2, 3},  // Row 0
    {4, 5, 6, 7},  // Row 1
    {8, 9, 10, 11}, // Row 2
    {12, 13, 14, 15} // Row 3
};

ตัวอย่าง

using System;

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] a = new int[5, 2] {{77,34}, {55,65}, {47,66}, {45,98}, {86,23} };
            int i, j;
            for (i = 0; i < 5; i++)
            {
                for (j = 0; j < 2; j++)
                {
                    Console.WriteLine(a[i,j]);
                }
            }
            Console.ReadKey();
        }
    }
}