3
我有SortedList降序。C#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创建的代码工作正常,没有异常发生。
@Saeed:请不要错误地编辑这篇文章。 – Ani
对不起,我没有阅读你的描述我犯了你的第一个和第二个样本之间的错误。 –