在C#中,排序列表类是用来表示键/值对的集合,这些键/值对按照键值进行排序,并且可以通过键或索引访问集合中的各个项。
我们可以将排序列表看作是数组和哈希表的组合,其中包含了可以使用键或索引访问各项的列表。如果你使用索引访问各项,那么它就是一个动态数组,如果你使用键来访问各项,那么它就是一个哈希表。另外集合中的各项总是按键值进行排序。
一、排序列表类中的属性
下表列出了排序列表类中一些常用的属性:
二、排序列表中的方法
示例代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace _010
{
internal class Program
{
static void Main(string[] args)
{
SortedList sl = new SortedList();
sl.Add("001", "小名");
sl.Add("002", "小case");
sl.Add("003", "小强");
if(sl.ContainsValue("张三"))
{
Console.WriteLine("该学生姓名已存在");
}
else
{
sl.Add("004", "张三");
}
//获取键的集合
ICollection key = sl.Keys;
foreach(string k in key)
{
Console.WriteLine(k + ":" + sl[k]);
}
Console.ReadKey();
}
}
}