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

วิธีการกำหนดอาร์เรย์มิติเดียว (Single-dimensional Array) ใน C#

อาร์เรย์ใช้เก็บชุดข้อมูล แต่คิดว่ามันเป็นชุดตัวแปรชนิดเดียวกันที่เก็บในหน่วยความจำติดกันจะเข้าใจง่ายกว่า

การกำหนดอาร์เรย์มิติเดียว:

int[] runs = new int[10];

กำหนดค่าเริ่มต้นในบรรทัดเดียว:

int[] runs = new int[5] {125, 173, 190, 264, 188};

ตัวอย่างเต็ม

using System;

namespace Program {
    class Demo {
        static void Main(string[] args) {
            int[] runs = new int[5] {125, 173, 190, 264, 188};
            int i, j;
            for (j = 0; j < 5; j++) {
                Console.WriteLine("Innings score of Cricketer[{0}] = {1}", j, runs[j]);
            }
            Console.ReadKey();
        }
    }
}

ผลลัพธ์

Innings score of Cricketer[0] = 125
Innings score of Cricketer[1] = 173
Innings score of Cricketer[2] = 190
Innings score of Cricketer[3] = 264
Innings score of Cricketer[4] = 188