คลาส SortedSet ใน C # แสดงถึงคอลเล็กชันของอ็อบเจ็กต์ที่ได้รับการดูแลตามลำดับการเรียงลำดับ
ต่อไปนี้เป็นคุณสมบัติของคลาส SortedSet -
| Sr.No | คุณสมบัติ &คำอธิบาย |
|---|---|
| 1 | ตัวเปรียบเทียบ รับวัตถุ IComparer |
| 2 | นับ รับจำนวนองค์ประกอบใน SortedSet |
| 3 | แม็กซ์ รับค่าสูงสุดใน SortedSet |
| 4 | ขั้นต่ำ รับค่าต่ำสุดใน SortedSet |
ต่อไปนี้เป็นวิธีการบางอย่างของคลาส SortedSet -
| Sr.No | วิธีการ &คำอธิบาย |
|---|---|
| 1 | เพิ่ม(T) เพิ่มองค์ประกอบในชุดและส่งกลับค่าที่ระบุว่าได้เพิ่มสำเร็จหรือไม่ |
| 2 | ล้าง() ลบองค์ประกอบทั้งหมดออกจากชุด |
| 3 | ประกอบด้วย(T) กำหนดว่าชุดมีองค์ประกอบเฉพาะหรือไม่ |
| 4 | CopyTo(T[]) คัดลอก SortedSet |
| 5 | CopyTo(T[], Int32) คัดลอก SortedSet |
| 6 | CopyTo(T[], Int32, Int32) คัดลอกองค์ประกอบตามจำนวนที่ระบุจาก SortedSet |
| 7 | CreateSetComparer() ส่งกลับวัตถุ IEqualityComparer ที่สามารถใช้เพื่อสร้างคอลเลกชันที่มีแต่ละชุด |
ตัวอย่าง
เรามาดูตัวอย่างกัน −
หากต้องการตรวจสอบว่า SortedSet มีองค์ประกอบเฉพาะหรือไม่ รหัสจะเป็นดังนี้ -
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
SortedSet<string> set1 = new SortedSet<string>();
set1.Add("CD");
set1.Add("CD");
set1.Add("CD");
set1.Add("CD");
Console.WriteLine("Elements in SortedSet1...");
foreach (string res in set1) {
Console.WriteLine(res);
}
Console.WriteLine("Does the SortedSet1 contains the element DE? = "+set1.Contains("DE"));
SortedSet<string> set2 = new SortedSet<string>();
set2.Add("BC");
set2.Add("CD");
set2.Add("DE");
set2.Add("EF");
set2.Add("AB");
set2.Add("HI");
set2.Add("JK");
Console.WriteLine("Elements in SortedSet2...");
foreach (string res in set2) {
Console.WriteLine(res);
}
Console.WriteLine("SortedSet2 is a superset of SortedSet1? = "+set2.IsSupersetOf(set1));
}
} ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
Elements in SortedSet1... CD Does the SortedSet1 contains the element DE? = False Elements in SortedSet2... AB BC CD DE EF HI JK SortedSet2 is a superset of SortedSet1? = True
เพื่อให้ได้ตัวแจงนับที่วนซ้ำผ่าน SortedSet รหัสจะเป็นดังนี้ -
ตัวอย่าง
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
SortedSet<string> set1 = new SortedSet<string>();
set1.Add("AB");
set1.Add("BC");
set1.Add("CD");
set1.Add("EF");
Console.WriteLine("Elements in SortedSet1...");
foreach (string res in set1) {
Console.WriteLine(res);
}
SortedSet<string> set2 = new SortedSet<string>();
set2.Add("BC");
set2.Add("CD");
set2.Add("DE");
set2.Add("EF");
set2.Add("AB");
set2.Add("HI");
set2.Add("JK");
Console.WriteLine("Elements in SortedSet2 (Enumerator for SortedSet)...");
SortedSet<string>.Enumerator demoEnum = set2.GetEnumerator();
while (demoEnum.MoveNext()) {
string res = demoEnum.Current;
Console.WriteLine(res);
}
}
} ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
Elements in SortedSet1... AB BC CD EF Elements in SortedSet2 (Enumerator for SortedSet)... AB BC CD DE EF HI JK