Giới thiệu
SortedList (non-generic, namespace System.Collections) lưu cặp key-value **tự động sắp xếp theo key**. Phương thức Clone() tạo **shallow copy**: SortedList mới với cùng các tham chiếu key/value.
Ví dụ 1: Clone SortedList
using System;
using System.Collections;
public class Demo
{
public static void Main(string[] args)
{
SortedList list = new SortedList();
list.Add("A", "Jacob"); list.Add("B", "Sam"); list.Add("C", "Tom");
list.Add("D", "John"); list.Add("E", "Tim"); list.Add("F", "Mark");
list.Add("G", "Gary"); list.Add("H", "Nathan"); list.Add("I", "Shaun");
list.Add("J", "David");
Console.WriteLine("SortedList gốc:");
foreach (DictionaryEntry d in list)
Console.WriteLine($"{d.Key} {d.Value}");
// Lấy Keys và Values collection
Console.WriteLine("\nValues:");
foreach (string s in list.Values) Console.WriteLine(s);
Console.WriteLine("\nKeys:");
foreach (string s in list.Keys) Console.WriteLine(s);
// Shallow copy
SortedList list2 = (SortedList)list.Clone();
Console.WriteLine("\nCloned SortedList:");
foreach (DictionaryEntry d in list2)
Console.WriteLine($"{d.Key} {d.Value}");
}
}
Kết quả
SortedList gốc:
A Jacob
B Sam
C Tom
D John
E Tim
F Mark
G Gary
H Nathan
I Shaun
J David
Values:
Jacob
Sam
Tom
John
Tim
Mark
Gary
Nathan
Shaun
David
Keys:
A
B
C
D
E
F
G
H
I
J
Cloned SortedList:
A Jacob
B Sam
C Tom
D John
E Tim
F Mark
G Gary
H Nathan
I Shaun
J David
Ví dụ 2: Clone sau khi dùng GetValueList
using System;
using System.Collections;
public class Demo
{
public static void Main(string[] args)
{
SortedList list = new SortedList();
list.Add("One", "IT");
list.Add("Two", "Operations");
list.Add("Three", "Marketing");
list.Add("Four", "Purchase");
list.Add("Five", "Sales");
list.Add("Six", "Finance");
Console.WriteLine("SortedList (sorted by key):");
foreach (DictionaryEntry d in list)
Console.WriteLine($"{d.Key} {d.Value}");
Console.WriteLine("\nValues (IList từ GetValueList):");
IList values = list.GetValueList();
foreach (string v in values) Console.WriteLine(v);
Console.WriteLine($"\nIsReadOnly: {list.IsReadOnly}"); // False
SortedList list2 = (SortedList)list.Clone();
Console.WriteLine("\nCloned:");
foreach (DictionaryEntry d in list2)
Console.WriteLine($"{d.Key} {d.Value}");
}
}
Kết quả
SortedList (sorted by key):
Five Sales
Four Purchase
One IT
Six Finance
Three Marketing
Two Operations
Values (IList từ GetValueList):
Sales
Purchase
IT
Finance
Marketing
Operations
IsReadOnly: False
Cloned:
Five Sales
Four Purchase
One IT
Six Finance
Three Marketing
Two Operations
Shallow Copy vs Deep Copy
- Shallow (Clone): Key và Value là reference type -> cùng tham chiếu. Key thường là string (immutable) -> an toàn. Value nếu là class -> thay đổi ảnh hưởng cả hai.
- Deep Copy: Phải tự serialize/deserialize hoặc recursive copy.
Lưu ý
SortedListnon-generic -> legacy. DùngSortedList<TKey, TValue>(generic) cho code mới.SortedList<TKey, TValue>**không có Clone()**. Shallow copy:new SortedList<TKey, TValue>(original)(copy constructor).- Key không được null, không trùng. Value có thể null.
- Phức tạp: Add/Remove O(n) (dịch mảng), IndexOfKey O(log n) (binary search).