Giới thiệu
OrderedDictionary (namespace System.Collections.Specialized) là từ điển **giữ thứ tự chèn** (insertion order). Cho phép truy xuất bằng key **hoặc** index. Phương thức Insert(int index, object key, object value) chèn cặp key-value tại vị trí chỉ định.
Ví dụ 1: Insert tại vị trí cuối (index = Count)
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");
Console.WriteLine("Ban đầu:");
foreach (DictionaryEntry d in dict) Console.WriteLine($"{d.Key} {d.Value}");
Console.WriteLine($"Count: {dict.Count}");
// Chèn tại index 4 (sau phần tử cuối)
dict.Insert(4, "E", "Clothing");
dict.Insert(5, "F", "Footwear");
Console.WriteLine("\nSau Insert:");
foreach (DictionaryEntry d in dict) Console.WriteLine($"{d.Key} {d.Value}");
Console.WriteLine($"Count: {dict.Count}");
dict.Remove("E");
Console.WriteLine($"\nSau Remove('E'), Count: {dict.Count}");
}
}
Kết quả
Ban đầu:
A Books
B Electronics
C Smart Wearables
D Pet Supplies
Count: 4
Sau Insert:
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
Count: 6
Sau Remove('E'), Count: 5
Ví dụ 2: Insert vào giữa, so sánh hai OrderedDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo
{
public static void Main()
{
OrderedDictionary dict1 = new OrderedDictionary();
dict1.Add("A", "Books"); dict1.Add("B", "Electronics");
dict1.Add("C", "Smart Wearables"); dict1.Add("D", "Pet Supplies");
dict1.Add("E", "Clothing"); dict1.Add("F", "Footwear");
OrderedDictionary dict2 = new OrderedDictionary();
dict2.Add("1", "One"); dict2.Add("2", "Two"); dict2.Add("3", "Three");
dict2.Add("4", "Four"); dict2.Add("5", "Five"); dict2.Add("6", "Six");
Console.WriteLine($"dict1 == dict2? {dict1.Equals(dict2)}"); // False
// Chèn "10 Ten" tại index 3 (trước "Four")
dict2.Insert(3, "10", "Ten");
Console.WriteLine("\nDict2 sau Insert(3, '10', 'Ten'):");
foreach (DictionaryEntry d in dict2) Console.WriteLine($"{d.Key} {d.Value}");
Console.WriteLine($"\ndict1 == dict2? {dict1.Equals(dict2)}"); // Vẫn False
}
}
Kết quả
dict1 == dict2? False
Dict2 sau Insert(3, '10', 'Ten'):
1 One
2 Two
3 Three
10 Ten
4 Four
5 Five
6 Six
dict1 == dict2? False
Lưu ý quan trọng
indexphải trong khoảng[0, Count].index == Counttương đươngAdd.- Key **không được trùng**. Ném
ArgumentExceptionnếu key đã tồn tại. - Phần tử từ
indextrở đi bị dịch sang phải. OrderedDictionarykhông generic (key/value là object). Hiện nay ít dùng, thay bằngDictionary<TKey, TValue>+ list key riêng, hoặcSystem.Collections.Generic.OrderedDictionary(third-party) hoặc .NET 8+FrozenDictionary(read-only).- Không thread-safe.
So sánh truy xuất
OrderedDictionary od = new OrderedDictionary();
od.Add("A", 1); od.Add("B", 2); od.Add("C", 3);
// Truy xuất bằng key
Console.WriteLine(od["B"]); // 2
// Truy xuất bằng index
Console.WriteLine(od[1]); // 2 (cùng giá trị)
Console.WriteLine(((DictionaryEntry)od[1]).Key); // "B"