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

วิธีสร้างอาร์เรย์สี่เหลี่ยมผืนผ้า (Rectangular Array) ใน C#

อาร์เรย์หลายมิติ (Multi-dimensional arrays) เรียกอีกอย่างว่า Rectangular Array เริ่มต้นค่าโดยระบุค่าในวงเล็บสำหรับแต่ละแถว

ตัวอย่างอาร์เรย์ 2 แถว 2 คอลัมน์:

int[,] a = new int[2,2] {
    {20, 50}, // initializers for row indexed by 0
    {15, 45}  // initializers for row indexed by 1
};

ตัวอย่าง

using System;
namespace Demo {
    class Program {
        static void Main(string[] args) {
            int[,] a = new int[2,2] {
                {20, 50},
                {15, 45}
            };
            int i, j;
            for (i = 0; i < 2; i++) {
                for (j = 0; j < 2; j++) {
                    Console.WriteLine("a[{0},{1}] = {2}", i, j, a[i,j]);
                }
            }
            Console.ReadKey();
        }
    }
}