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

Truy xuất phần tử từ Collection trong C#

Ví dụ List collection:

Thiết lập phần tử:

List list = new List();
list.Add(20);
list.Add(40);
list.Add(60);
list.Add(80);

Lấy phần tử đầu tiên (index 0):

int a = list[0];

Ví dụ

using System;
using System.Collections.Generic;

class Demo {
    static void Main(String[] args) {
        List list = new List();
        list.Add(20);
        list.Add(40);
        list.Add(60);
        list.Add(80);
        foreach (int val in list) {
            Console.WriteLine(val);
        }
        int a = list[0];
        Console.WriteLine("Phần tử đầu tiên: " + a);
    }
}