คลาส SortedList ใน C# แสดงถึงคอลเล็กชันของคู่คีย์/ค่าที่จัดเรียงตามคีย์และสามารถเข้าถึงได้ตามคีย์และตามดัชนี
ต่อไปนี้เป็นคุณสมบัติของคลาส SortedList -
| Sr.No | คุณสมบัติ &คำอธิบาย |
|---|---|
| 1 | ความจุ รับหรือตั้งค่าความจุของวัตถุ SortedList |
| 2 | นับ รับจำนวนองค์ประกอบที่มีอยู่ในวัตถุ SortedList |
| 3 | IsFixedSize รับค่าที่ระบุว่าอ็อบเจ็กต์ SortedList มีขนาดคงที่หรือไม่ |
| 4 | เป็นแบบอ่านอย่างเดียว รับค่าที่ระบุว่าอ็อบเจ็กต์ SortedList เป็นแบบอ่านอย่างเดียวหรือไม่ |
| 5 | ซิงโครไนซ์ รับค่าที่ระบุว่าการเข้าถึงอ็อบเจ็กต์ SortedList ถูกซิงโครไนซ์หรือไม่ (thread safe) |
| 6 | รายการ[วัตถุ] รับหรือตั้งค่าที่เกี่ยวข้องกับคีย์เฉพาะในอ็อบเจ็กต์ SortedList |
| 7 | กุญแจ รับคีย์ในอ็อบเจ็กต์ SortedList |
| 8 | SyncRoot รับอ็อบเจ็กต์ที่สามารถใช้เพื่อซิงโครไนซ์การเข้าถึงอ็อบเจ็กต์ SortedList |
| 9 | ค่า รับค่าในอ็อบเจ็กต์ SortedList |
ต่อไปนี้เป็นวิธีการบางส่วนของคลาส Sorted -
| Sr.No | วิธีการ &คำอธิบาย |
|---|---|
| 1 | เพิ่ม (วัตถุ, วัตถุ) เพิ่มองค์ประกอบด้วยคีย์และค่าที่ระบุให้กับอ็อบเจ็กต์ SortedList |
| 2 | ล้าง() ลบองค์ประกอบทั้งหมดออกจากวัตถุ SortedList |
| 3 | โคลน() สร้างสำเนาตื้นของวัตถุ SortedList |
| 4 | ประกอบด้วย (วัตถุ) กำหนดว่าอ็อบเจ็กต์ SortedList มีคีย์เฉพาะหรือไม่ |
| 5 | ContainsKey(Object) กำหนดว่าอ็อบเจ็กต์ SortedList มีคีย์เฉพาะหรือไม่ |
| 6 | ContainsValue(Object) กำหนดว่าอ็อบเจ็กต์ SortedList มีค่าเฉพาะหรือไม่ |
| 7 | CopyTo(Array, Int32) คัดลอกองค์ประกอบ SortedList ไปยังวัตถุ onedimensionalArray โดยเริ่มต้นที่ดัชนีที่ระบุในอาร์เรย์ |
| 8 | SyncRoot รับอ็อบเจ็กต์ที่สามารถใช้เพื่อซิงโครไนซ์การเข้าถึงอ็อบเจ็กต์ SortedList |
| 9 | ค่า รับค่าในอ็อบเจ็กต์ SortedList |
เรามาดูตัวอย่างกัน −
เพื่อให้ได้จำนวนองค์ประกอบที่มีอยู่ใน 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 สองรายการเท่ากันหรือไม่ รหัสจะเป็นดังนี้ -
ตัวอย่าง
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