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

Key-based I/O Collections ใน C# (SortedList)

Key-based I/O Collection ใน C# คือ SortedList<TKey, TValue>

SortedList = Key-Value Pairs เรียงตาม Key เข้าถึงได้ทั้ง Key และ Index

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

ตัวอย่าง

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]);
        }
    }
}

Output

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