I see this question.
How can I get the last element in a SortedDictionary in .Net 3.5.
You can use LINQ:
var lastItem = sortedDict.Values.Last();
You can also get the last key:
var lastkey = sortedDict.Keys.Last();
You can even get the last key-value pair:
var lastKeyValuePair = sortedDict.Last();
This will give you a KeyValuePair<TKey, TValue>
with Key
and Value
properties.
Note that this will throw an exception if the dictionary is empty; if you don't want that, call LastOrDefault
.