C#中的SortedList类表示键/值对的集合,这些键/值对按键排序,并且可以通过键和索引进行访问。
以下是SortedList类的属性-
| 序号 | 属性和说明 |
|---|---|
| 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 | 值 获取SortedList对象中的值。 |
以下是Sorted类的一些方法-
| 序号 | 方法与说明 |
|---|---|
| 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元素复制到一维Array对象中。 |
| 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("\nSortedList2 elements...");
foreach(DictionaryEntry d in list2) {
Console.WriteLine(d.Key + " " + d.Value);
}
SortedList list3 = new SortedList();
list3 = list2;
Console.WriteLine("\nIs 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