Phương thức SortedDictionary<TKey, TValue>.Clear() trong C# được dùng để xóa tất cả các phần tử khỏi SortedDictionary. Sau khi gọi, Count bằng 0, nhưng dung lượng nội bộ có thể được giữ lại để tái sử dụng.
Cú pháp
public void Clear();
Ví dụ 1: Clear sau khi thêm 6 phần tử
using System;
using System.Collections;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
SortedDictionary sortedDict = new SortedDictionary();
sortedDict.Add(100, "Mobile");
sortedDict.Add(200, "Laptop");
sortedDict.Add(300, "Desktop");
sortedDict.Add(400, "Speakers");
sortedDict.Add(500, "Headphone");
sortedDict.Add(600, "Earphone");
Console.WriteLine("Các cặp key-value...");
IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
while (demoEnum.MoveNext())
{
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
Console.WriteLine("Số lượng cặp = " + sortedDict.Count);
Console.WriteLine("\nCó key 200? = " + sortedDict.ContainsKey(200));
sortedDict.Clear();
Console.WriteLine("Số lượng sau Clear = " + sortedDict.Count);
}
}
Kết quả
Key = 100, Value = Mobile Key = 200, Value = Laptop Key = 300, Value = Desktop Key = 400, Value = Speakers Key = 500, Value = Headphone Key = 600, Value = Earphone Số lượng cặp = 6 Có key 200? = True Số lượng sau Clear = 0
Ví dụ 2: Thêm, Clear, Count
using System;
using System.Collections;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
SortedDictionary sortedDict = new SortedDictionary();
sortedDict.Add(100, "Inspiron");
sortedDict.Add(200, "Alienware");
sortedDict.Add(300, "Projectors");
sortedDict.Add(400, "XPS");
Console.WriteLine("Các cặp key-value...");
IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
while (demoEnum.MoveNext())
{
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
Console.WriteLine("Số lượng = " + sortedDict.Count);
sortedDict.Add(800, "Notebook");
sortedDict.Add(10000, "Bluetooth Speaker");
Console.WriteLine("\nCác cặp key-value SAU KHI CẬP NHẬT...");
demoEnum = sortedDict.GetEnumerator();
while (demoEnum.MoveNext())
{
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
Console.WriteLine("Số lượng (cập nhật) = " + sortedDict.Count);
sortedDict.Clear();
Console.WriteLine("\nSố lượng sau Clear = " + sortedDict.Count);
}
}
Kết quả
Key = 100, Value = Inspiron Key = 200, Value = Alienware Key = 300, Value = Projectors Key = 400, Value = XPS Số lượng = 4 Các cặp key-value SAU KHI CẬP NHẬT... Key = 100, Value = Inspiron Key = 200, Value = Alienware Key = 300, Value = Projectors Key = 400, Value = XPS Key = 800, Value = Notebook Key = 10000, Value = Bluetooth Speaker Số lượng (cập nhật) = 6 Số lượng sau Clear = 0
Clear() vs new Instance vs Remove()
| Cách xóa | Hành vi | Dung lượng (Capacity) |
|---|---|---|
Clear() |
Xóa tất cả phần tử, Count = 0 | Giữ nguyên (tối ưu tái sử dụng) |
new SortedDictionary<TKey,TValue>() |
Tạo instance mới hoàn toàn | Mặc định (thường 0 hoặc nhỏ) |
Remove(key) lặp |
Xóa từng cái một | Giảm dần, chậm hơn Clear |
Lưu ý
Clear()là O(n) (phải dọn dẹp node tree), nhưng nhanh hơn Remove lặp.- Sau Clear,
Keys,Valuestrả về collection rỗng. - Enumerator đang hoạt động sẽ bị invalid (ném
InvalidOperationExceptionkhi MoveNext). - Không giải phóng bộ nhớ managed (GC sẽ lo), chỉ reset cấu trúc tree nội bộ.
Khi nào dùng Clear()?
- Cần tái sử dụng SortedDictionary instance (tránh allocate mới).
- Reset cache, buffer, state giữa các batch xử lý.
- Kết hợp với
TrimExcess(nếu có) để giải phóng memory - nhưng SortedDictionary không có TrimExcess.