C# 中 SortedList 的示例?
csharpserver side programmingprogramming更新于 2025/9/18 21:52:17
C# 中的 SortedList 类表示按键排序的键/值对集合,可通过键和索引访问。
以下是 SortedList 类的属性 −
| Sr.No | 属性 &说明 |
|---|---|
| 1 | Capacity 获取或设置 SortedList 对象的容量。 |
| 2 | Count 获取 SortedList 对象中包含的元素数量。 |
| 3 | IsFixedSize 获取一个值,该值指示 SortedList 对象是否具有固定大小。 |
| 4 | IsReadOnly 获取一个值,该值指示 SortedList 对象是否为只读。 |
| 5 | IsSynchronized 获取一个值,该值指示对 SortedList 对象的访问是否同步(线程安全)。 |
| 6 | Item[Object] 获取或设置与 SortedList 对象中特定键关联的值。 |
| 7 | Keys 获取 SortedList 对象中的键。 |
| 8 | SyncRoot 获取可用于同步对 SortedList 对象的访问的对象。 |
| 9 | Values 获取SortedList 对象中的值。 |
以下是 Sorted 类的一些方法 −
| Sr.No | 方法 &说明 |
|---|---|
| 1 | Add(Object, Object) 将具有指定键和值的元素添加到 SortedList 对象。 |
| 2 | Clear() 从 SortedList 对象中删除所有元素。 |
| 3 | Clone() 创建 SortedList 对象的浅表副本。 |
| 4 | Contains(Object) 确定 SortedList 对象是否包含特定键。 |
| 5 | ContainsKey(Object) 判断 SortedList 对象是否包含特定键。 |
| 6 | ContainsValue(Object) 判断 SortedList 对象是否包含特定值。 |
| 7 | CopyTo(Array, Int32) 将 SortedList 元素复制到一维数组对象,从数组中的指定索引开始。 |
| 8 | SyncRoot 获取可用于同步对 SortedList 对象访问的对象。 |
| 9 | 值 获取 SortedList 对象中的值。 |
现在让我们看一些示例 −
要获取 SortedList 中包含的元素数量,代码如下 −
示例
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("Count of SortedList key-value pairs = "+sortedList.Count);
sortedList.Clear();
Console.WriteLine("Count of SortedList (updated) = "+sortedList.Count);
}
}
输出
这将产生以下输出 −
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 Count of SortedList key-value pairs = 10 Count of SortedList (updated) = 0
检查两个 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("
SortedList2 elements...");
foreach(DictionaryEntry d in list2) {
Console.WriteLine(d.Key + " " + d.Value);
}
SortedList list3 = new SortedList();
list3 = list2;
Console.WriteLine("
Is SortedList2 equal to SortedList3? = "+list3.Equals(list2));
}
}
输出
这将产生以下输出 −
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 Is SortedList2 equal to SortedList3? = True

