Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

SortedList với các ví dụ trong C#

Lớp SortedList trong C# biểu diễn tập hợp các cặp key/value được sắp xếp theo key, truy xuất được bằng key và index.

Các thuộc tính của SortedList

STTThuộc tính & Mô tả
1Capacity
Lấy hoặc đặt dung lượng của SortedList.
2Count
Lấy số phần tử chứa trong SortedList.
3IsFixedSize
Cho biết SortedList có kích thước cố định hay không.
4IsReadOnly
Cho biết SortedList có chỉ đọc hay không.
5IsSynchronized
Cho biết truy cập SortedList có đồng bộ (thread-safe) hay không.
6Item[Object]
Lấy hoặc đặt giá trị liên kết với key cụ thể.
7Keys
Lấy các key trong SortedList.
8SyncRoot
Lấy object dùng để đồng bộ truy cập SortedList.
9Values
Lấy các value trong SortedList.

Một số phương thức của SortedList

STTPhương thức & Mô tả
1Add(Object, Object)
Thêm phần tử với key và value chỉ định.
2Clear()
Xóa tất cả phần tử.
3Clone()
Tạo bản sao nông (shallow copy).
4Contains(Object)
Kiểm tra SortedList có chứa key cụ thể.
5ContainsKey(Object)
Kiểm tra SortedList có chứa key cụ thể.
6ContainsValue(Object)
Kiểm tra SortedList có chứa value cụ thể.
7CopyTo(Array, Int32)
Sao chép phần tử SortedList sang mảng 1 chiều từ index chỉ định.
8GetEnumerator()
Trả về IDictionaryEnumerator để duyệt.
9IndexOfValue(Object)
Trả về index của value đầu tiên tìm thấy.

Ví dụ 1: Lấy số phần tử và Clear

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("Số cặp key-value = " + sortedList.Count);
        sortedList.Clear();
        Console.WriteLine("Số cặp key-value (sau Clear) = " + sortedList.Count);
    }
}

Kết quả

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
Số cặp key-value = 10
Số cặp key-value (sau Clear) = 0

Ví dụ 2: So sánh hai 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("\nSortedList2 bằng SortedList3? = " + list3.Equals(list2));
    }
}

Kết quả

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
SortedList2 bằng SortedList3? = True