C# 中的 SortedList 类是什么?

C# 中的 SortedList 类是什么?

A sorted list is a combination of an array and a hash table. It contains a list of items that can be accessed using a key or an index. If you access items using an index, it is an ArrayList, and if you access items using a key, it is a Hashtable. The collection of items is always sorted by the key value.

Let us see an example wherein we added 4 key and value pair for SortedList −

Example

using System;
using System.Collections;

namespace Demo {
class Program {
static void Main(string[] args) {
SortedList s = new SortedList();

s.Add("S1", "Maths");
s.Add("S2", "Science");
s.Add("S3", "English");
s.Add("S4", "Economics");
}
}
}

登录后复制

让我们现在来看看如何获取SortedList的键并显示它 −

示例

using System;
using System.Collections;

namespace Demo {
class Program {
static void Main(string[] args) {
SortedList sl = new SortedList();

sl.Add("ST0", "One");
sl.Add("ST1", "Two");
sl.Add("ST2", "Three");
sl.Add("ST3", "Four");
sl.Add("ST4", "Five");
sl.Add("ST5", "Six");
sl.Add("ST6", "Seven");

ICollection key = sl.Keys;

foreach (string k in key) {
Console.WriteLine(k);
}
}
}
}

登录后复制

以上就是C# 中的 SortedList 类是什么?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!