Để xóa tất cả các cặp key-value khỏi OrderedDictionary trong C#, bạn sử dụng phương thức Clear(). Khác với Hashtable hoặc Dictionary, OrderedDictionary duy trì thứ tự thêm vào (insertion order) của các phần tử.
Ví dụ 1: Xóa tất cả từ OrderedDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo
{
public static void Main()
{
OrderedDictionary dict = new OrderedDictionary();
dict.Add("A", "Books");
dict.Add("B", "Electronics");
dict.Add("C", "Smart Wearables");
dict.Add("D", "Pet Supplies");
dict.Add("E", "Clothing");
dict.Add("F", "Footwear");
Console.WriteLine("Các phần tử của OrderedDictionary...");
foreach (DictionaryEntry d in dict)
{
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("Số lượng phần tử trong OrderedDictionary = " + dict.Count);
// Xóa tất cả phần tử
dict.Clear();
Console.WriteLine("Số lượng phần tử trong OrderedDictionary (đã cập nhật) = " + dict.Count);
}
}
Kết quả
Các phần tử của OrderedDictionary...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
Số lượng phần tử trong OrderedDictionary = 6
Số lượng phần tử trong OrderedDictionary (đã cập nhật) = 0
Ví dụ 2: OrderedDictionary nhỏ
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo
{
public static void Main()
{
OrderedDictionary dict = new OrderedDictionary();
dict.Add("1", "AB");
dict.Add("2", "CD");
Console.WriteLine("Các phần tử của OrderedDictionary...");
foreach (DictionaryEntry d in dict)
{
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("Số lượng phần tử trong OrderedDictionary = " + dict.Count);
dict.Clear();
Console.WriteLine("Số lượng phần tử trong OrderedDictionary (đã cập nhật) = " + dict.Count);
}
}
Kết quả
Các phần tử của OrderedDictionary...
1 AB
2 CD
Số lượng phần tử trong OrderedDictionary = 2
Số lượng phần tử trong OrderedDictionary (đã cập nhật) = 0
Đặc điểm của OrderedDictionary
| Đặc điểm |
Mô tả |
| Thứ tự |
Giữ thứ tự thêm vào (Insertion Order) |
| Truy xuất |
Bằng key (O(1)) hoặc index (O(1)) |
| Key trùng lặp |
Không cho phép |
| Key null |
Không cho phép |
| Namespace |
System.Collections.Specialized |
Truy xuất theo Index (đặc quyền của OrderedDictionary)
OrderedDictionary dict = new OrderedDictionary();
dict.Add("A", "Apple");
dict.Add("B", "Banana");
dict.Add("C", "Cherry");
// Truy xuất bằng index (0-based)
Console.WriteLine(dict[0]); // "Apple" - value tại index 0
Console.WriteLine(dict[1]); // "Banana"
// Truy xuất bằng key
Console.WriteLine(dict["B"]); // "Banana"
// Lấy key tại index
Console.WriteLine(dict.Keys[1]); // "B"
So sánh với các Dictionary khác
| Loại |
Thứ tự |
Truy xuất Index |
Type-safe |
| Hashtable |
Không |
Không |
Không |
| Dictionary<TKey,TValue> |
Không đảm bảo |
Không |
Có |
| SortedDictionary |
Sắp xếp key |
Không |
Có |
| OrderedDictionary |
Thêm vào |
Có |
Không (object) |
Lưu ý
OrderedDictionary không phải generic - key/value là object
- Đã lỗi thời - cân nhắc
Dictionary<TKey, TValue> + danh sách key riêng nếu cần thứ tự
- Hoặc dùng
System.Collections.Generic.OrderedDictionary<TKey, TValue> (từ .NET 6+ trong namespace System.Collections.Generic)
Clear() chạy O(n), đặt Count = 0
Alternatives hiện đại
// .NET 6+ - Generic OrderedDictionary (experimental)
// using System.Collections.Generic;
// var dict = new OrderedDictionary<string, string>();
// Hoặc dùng Dictionary + List để giữ thứ tự
var dict = new Dictionary<string, string>();
var keys = new List<string>();
dict["A"] = "Apple";
keys.Add("A");