Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Truy cập phần tử mảng (Array) trong C#


Khởi tạo mảng: int[] p = new int[3] {99, 92, 95};

Duyệt in:

for (j=0; j<3; j++) Console.WriteLine("Price[{0}] = {1}", j, p[j]);

Truy cập phần tử bất kỳ bằng index: p[2] (phần tử thứ 3).

Ví dụ

using System;
namespace Program {
    class Demo {
        static void Main(string[] args) {
            int[] p = new int[3] {99, 92, 95};
            for (int j=0; j<3; j++) Console.WriteLine("Price[{0}] = {1}", j, p[j]);
            int e = p[2]; Console.WriteLine("Phần tử 3: " + e);
            Console.ReadKey();
        }
    }
}