รายการที่เรียงลำดับคือการรวมกันของอาร์เรย์และตารางแฮช ประกอบด้วยรายการที่สามารถเข้าถึงได้โดยใช้คีย์หรือดัชนี หากคุณเข้าถึงรายการโดยใช้ดัชนี รายการนั้นจะเป็น ArrayList และหากคุณเข้าถึงรายการโดยใช้คีย์ รายการนั้นจะเป็น Hashtable คอลเลกชันของรายการจะถูกจัดเรียงตามค่าคีย์เสมอ
ให้เราดูตัวอย่างที่เราเพิ่ม 4 คู่คีย์และค่าสำหรับ SortedList -
ตัวอย่าง
using System;
using System.Collections;
namespace Demo {
class Program {
static void Main(string[] args) {
SortedList s = new SortedList();
s.Add("S1", "Maths");
s.Add("S2", "Science");
s.Add("S3", "English");
s.Add("S4", "Economics");
}
}
} ให้เรามาดูวิธีรับกุญแจของ SortedList และแสดงมัน -
ตัวอย่าง
using System;
using System.Collections;
namespace Demo {
class Program {
static void Main(string[] args) {
SortedList sl = new SortedList();
sl.Add("ST0", "One");
sl.Add("ST1", "Two");
sl.Add("ST2", "Three");
sl.Add("ST3", "Four");
sl.Add("ST4", "Five");
sl.Add("ST5", "Six");
sl.Add("ST6", "Seven");
ICollection key = sl.Keys;
foreach (string k in key) {
Console.WriteLine(k);
}
}
}
}