我按降序排列了 SortedList。
public class MyComparer : IComparer<int>
{
public int Compare(int x, int y)
{
if (x.CompareTo(y) > 0)
return -1;
return 1;
}
}
class Program
{
static void Main(string[] args)
{
SortedList<int, bool> myList = new SortedList<int, bool>(new MyComparer());
myList.Add(10, true);
bool check = myList[10];//In this place an exception "Can't find key" occurs
}
}
当在没有我自己的 IComparer 的情况下创建 SortedList 时,代码可以正常工作并且不会发生异常。