SortedDictionary ContainsKey方法错误
本文关键字:错误 方法 ContainsKey SortedDictionary | 更新日期: 2023-09-27 18:28:16
所以基本上我有这样的东西:
private SortedDictionary<Priority, List<String>> example = new SortedDictionary<Priority, List<String>>(new PriorityComparer());
public void Submit(Priority priority, String test)
{
if (!example.ContainsKey(priority))
{
example.Add(priority, new List<String> { test });
// adds an entry with the key (Priority.Low) twice, why?
}
else
{
example[priority].Add(test); // never called
}
}
只需提交一堆测试元素:
Submit(Priority.Critical, "test4");
Submit(Priority.Low, "test");
Submit(Priority.Normal, "test3");
Submit(Priority.Low, "test2");
Priority只是一个枚举,其优先级范围从Low(0)到Critical(10)。这只是一个典型的枚举,没有什么新奇之处。
优先级比较器:
public class PriorityComparer : IComparer<Priority>
{
public int Compare(Priority x, Priority y)
{
if (x > y)
{
return -1;
}
else
{
return 1;
}
}
}
您的比较实现不完整。当x==y(返回0)时,您错过了案例。