【发布时间】:2011-09-30 02:13:11
【问题描述】:
我必须尽快从 SortedDictionary 中删除第二个元素。字典 (SortedDictionary<string, List<string>>) 最多可以包含 20'000 个元素。所以我想出了这个解决方案:
try
{
int loop = 0;
while (true)
{
Routes.Remove(Routes.ElementAt(loop).Key);
loop++;
}
}
catch
{
}
还有比这更简单/更好的解决方案吗? 捕获的异常会对性能有影响吗?
编辑:这似乎是一个更好的解决方案(见下面的评论):
SortedDictionary<string, List<string>> resizedRoutes = new SortedDictionary<string, List<string>>();
bool b = true;
foreach(KeyValuePair<string, List<string>> route in Routes)
{
if(b)
{
resizedRoutes.Add(route.Key, route.Value);
b = false;
}
else
{
b = true;
}
}
Routes = resizedRoutes;
如果您有更好的解决方案,请编辑/评论。谢谢。
【问题讨论】:
标签: .net generics c#-4.0 dictionary sorteddictionary