我的问题就像下面的问题,但与SortedList<Tkey, TValue>
而不是原来的SortedList
。
Return first element in SortedList in C#
似乎没有像GetKey()
这样的功能。
你可以简单地使用list.FirstOrDefault()
和list.LastOrDefault()
。
如果列表为空,这两个方法将返回default(KeyValuePair<TKey,TValue>)
。
并且最好使用它们,因为使用list.First()
和list.Last()
会在列表为空时抛出错误。
根据您的(不太清楚)问题,您想要检索列表中第一个和最后一个项目的键。
SortedList<TKey, TValue>
实施IEnumerable<KeyValuePair<TKey,TValue>>
所以这样做:
sortedList.FirstOrDefault().Key
和
sortedList.LastOrDefault().Key
将返回这些键
编辑:正如@mjwills所建议的那样,使用Keys
属性在性能方面是一个更好的主意,因为它实现了IList<TKey>
和LastOrDefault
被优化以适用于这种情况(不是获取整个集合来获取最后一项):
sortedList.Keys.FirstOrDefault()
sortedList.Keys.LastOrDefault()
使用IEnumerable <>。Last或IEnumerable <>。LastOrDefault方法仅对IList <>值有效。让我们看看the source code - 访问非IList <>的最后一项需要迭代所有项目。
为了更高效率,更好地依赖于类型IList <>(SortedList<>.Keys)的SortedList<>.Values或https://dotnetfiddle.net/MLmDIL属性:
var sl = new SortedList<string, int>();
for (var i = 0; i < 50000; i++) {
sl.Add(i.ToString("X8"), i);
}
Console.WriteLine("SortedList<> is IList<>: " + (sl is IList<KeyValuePair<string, int>>));
Console.WriteLine("Keys-property in SortedList<> is IList<>: " + (sl.Keys is IList<string>));
Console.WriteLine("\n\nPerformance measurement - get last Key:");
var firstKey = sl.First().Key;
watch.Restart();
var lastKey = sl.Last().Key;
watch.Stop();
Console.WriteLine(" * Non IList<> access takes {0} (first key: {1}, last key: {2})", watch.Elapsed.TotalMilliseconds, firstKey, lastKey);
firstKey = sl.Keys.First();
watch.Restart();
lastKey = sl.Keys.Last();
watch.Stop();
Console.WriteLine(" * IList<> access takes {0} (first key: {1}, last key: {2})", watch.Elapsed.TotalMilliseconds, firstKey, lastKey);
Console.WriteLine("\n\nPerformance measurement - get last Value:");
var firstValue = sl.First().Value;
watch.Restart();
var lastValue = sl.Last().Value;
watch.Stop();
Console.WriteLine(" * Non IList<> access takes {0} (first value: {1}, last value: {2})", watch.Elapsed.TotalMilliseconds, firstValue, lastValue);
firstValue = sl.Values.First();
watch.Restart();
lastValue = sl.Values.Last();
watch.Stop();
Console.WriteLine(" * IList<> access takes {0} (first value: {1}, last value: {2})", watch.Elapsed.TotalMilliseconds, firstValue, lastValue);
Console.WriteLine("\n\nPerformance measurement - get last Value by Key:");
watch.Restart();
lastKey = sl.Keys.Last();
lastValue = sl[lastKey];
watch.Stop();
Console.WriteLine(" * IDictionary<> access takes {0} (last key: {1}, last value: {2})", watch.Elapsed.TotalMilliseconds, lastKey, lastValue);
/*
Execution result:
SortedList<> is IList<>: False
Keys-property in SortedList<> is IList<>: True
Performance measurement - get last Key:
* Non IList<> access takes 0.7146 (first key: 00000000, last key: 0000C34F)
* IList<> access takes 0.0032 (first key: 00000000, last key: 0000C34F)
Performance measurement - get last Value:
* Non IList<> access takes 0.7366 (first value: 0, last value: 49999)
* IList<> access takes 0.0003 (first value: 0, last value: 49999)
Performance measurement - get last Value by Key:
* IDictionary<> access takes 0.0036 (last key: 0000C34F, last value: 49999)
*/