views:

435

answers:

3

I see this question.

How can I get the last element in a SortedDictionary in .Net 3.5.

+1  A: 

You can use SortedDictionary.Values.Last();

or if you want the key and the value

SortedDictionary.Last();
Kevin
+4  A: 

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.

SLaks