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

Collection I/O dựa trên khóa (Key-based I/O Collections) trong C#

Key-based I/O Collection trong C# là SortedList<TKey,TValue>.

SortedList chứa cặp key-value được sắp xếp theo key, truy xuất được bằng key và index.

Thêm phần tử:

s.Add("Sub1", "Physics");
s.Add("Sub2", "Chemistry");
s.Add("Sub3", "Biology");
s.Add("Sub4", "Java");

Ví dụ

using System;
using System.Collections;

namespace Demo {
    class Program {
        static void Main(string[] args) {
            SortedList s = new SortedList();
            s.Add("Sub1", "Economics");
            s.Add("Sub2", "Accountancy");
            s.Add("Sub3", "Business Studies");
            s.Add("Sub4", "English");
            Console.WriteLine("Capacity = " + s.Capacity);
            ICollection key = s.Keys;
            foreach (string k in key) Console.WriteLine(k + ": " + s[k]);
        }
    }
}

Kết quả

Capacity = 16
Sub1: Economics
Sub2: Accountancy
Sub3: Business Studies
Sub4: English