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

SortedList ใน C# พร้อมตัวอย่างและคำอธิบาย


คลาส SortedList ใน C# แสดงถึงคอลเลกชันของคู่ Key/Value ที่เรียงลำดับตาม Key และเข้าถึงได้ทั้งด้วย Key และ Index

ต่อไปนี้คือ Properties ของคลาส SortedList:

ลำดับ Property & Description
1 Capacity
Get หรือ Set ความจุของ SortedList Object
2 Count
Get จำนวน Elements ใน SortedList Object
3 IsFixedSize
Get ว่า SortedList Object มีขนาดคงที่หรือไม่
4 IsReadOnly
Get ว่า SortedList Object เป็น Read-only หรือไม่
5 IsSynchronized
Get ว่าการเข้าถึง SortedList Object ถูก Synchronized (Thread-safe) หรือไม่
6 Item[Object]
Get หรือ Set ค่าที่เกี่ยวข้องกับ Key เฉพาะใน SortedList Object
7 Keys
Get Keys ใน SortedList Object
8 SyncRoot
Get Object ที่ใช้ Synchronize การเข้าถึง SortedList Object
9 Values
Get Values ใน SortedList Object

ต่อไปนี้คือ Methods หลักของ SortedList Class:

ลำดับ Methods & Description
1 Add(Object, Object)
เพิ่ม Element พร้อม Key และ Value ที่ระบุเข้า SortedList Object
2 Clear()
ลบ Elements ทั้งหมดออกจาก SortedList Object
3 Clone()
สร้าง Shallow Copy ของ SortedList Object
4 Contains(Object)
ตรวจสอบว่า SortedList Object มี Key เฉพาะนั้นหรือไม่
5 ContainsKey(Object)
ตรวจสอบว่า SortedList Object มี Key เฉพาะนั้นหรือไม่
6 ContainsValue(Object)
ตรวจสอบว่า SortedList Object มี Value เฉพาะนั้นหรือไม่
7 CopyTo(Array, Int32)
คัดลอก Elements ของ SortedList ไปยัง Array 1 มิติ เริ่มจาก Index ที่ระบุ
8 GetEnumerator()
ส่งคืน IDictionaryEnumerator สำหรับ SortedList Object
9 Remove(Object)
ลบ Element ที่มี Key ที่ระบุออกจาก SortedList Object

มาดูตัวอย่างการใช้งาน:

ตัวอย่าง: Get จำนวน Elements ใน SortedList

using System; 
using System.Collections; 
public class Demo { 
    public static void Main(String[] args) { 
        SortedList sortedList = new SortedList(); 
        sortedList.Add("A", "1"); 
        sortedList.Add("B", "2"); 
        sortedList.Add("C", "3"); 
        sortedList.Add("D", "4"); 
        sortedList.Add("E", "5"); 
        sortedList.Add("F", "6"); 
        sortedList.Add("G", "7"); 
        sortedList.Add("H", "8"); 
        sortedList.Add("I", "9"); 
        sortedList.Add("J", "10"); 
        Console.WriteLine("SortedList elements..."); 
        foreach(DictionaryEntry d in sortedList) { 
            Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value); 
        } 
        Console.WriteLine("Count of SortedList key-value pairs = " + sortedList.Count); 
        sortedList.Clear(); 
        Console.WriteLine("Count of SortedList (updated) = " + sortedList.Count); 
    } 
}

ผลลัพธ์

SortedList elements... 
Key = A, Value = 1 
Key = B, Value = 2 
Key = C, Value = 3 
Key = D, Value = 4 
Key = E, Value = 5 
Key = F, Value = 6 
Key = G, Value = 7 
Key = H, Value = 8 
Key = I, Value = 9 
Key = J, Value = 10 
Count of SortedList key-value pairs = 10 
Count of SortedList (updated) = 0

ตัวอย่าง: ตรวจสอบว่า SortedList 2 Object เท่ากันหรือไม่

using System; 
using System.Collections; 
public class Demo { 
    public static void Main(String[] args) { 
        SortedList list1 = new SortedList(); 
        list1.Add("One", 1); 
        list1.Add("Two", 2); 
        list1.Add("Three", 3); 
        list1.Add("Four", 4); 
        list1.Add("Five", 5); 
        list1.Add("Six", 6); 
        list1.Add("Seven", 7); 
        list1.Add("Eight", 8); 
        list1.Add("Nine", 9); 
        list1.Add("Ten", 10); 
        Console.WriteLine("SortedList1 elements..."); 
        foreach(DictionaryEntry d in list1) { 
            Console.WriteLine(d.Key + " " + d.Value); 
        } 
        SortedList list2 = new SortedList(); 
        list2.Add("A", "Accessories"); 
        list2.Add("B", "Books"); 
        list2.Add("C", "Smart Wearable Tech"); 
        list2.Add("D", "Home Appliances"); 
        Console.WriteLine("\nSortedList2 elements..."); 
        foreach(DictionaryEntry d in list2) { 
            Console.WriteLine(d.Key + " " + d.Value); 
        } 
        SortedList list3 = new SortedList(); 
        list3 = list2; 
        Console.WriteLine("\nIs SortedList2 equal to SortedList3? = " + list3.Equals(list2)); 
    } 
}

ผลลัพธ์

SortedList1 elements... 
Eight 8 
Five 5 
Four 4 
Nine 9 
One 1 
Seven 7 
Six 6 
Ten 10 
Three 3 
Two 2 
SortedList2 elements... 
A Accessories 
B Books 
C Smart Wearable Tech 
D Home Appliances 
Is SortedList2 equal to SortedList3? = True