Getting i-th value from a SortedList or SortedDictionary

You can extract value at a particular position by using the below syntax:

sortedDictionaryName.ElementAt(index);

If you want extract key or value of an element at a desired index:

sortedDictionaryName.ElementAt(index).Key //For only Key
sortedDictionaryName.ElementAt(index).Value //For only Value

Try something like this:

list.Values[list.Count / 2];

Note that a true median would average the two numbers in the middle if Count is even.


You can use code like

list.Values[index] 

for a sorted list.

The easiest way with a SortedDictonary would be to use the ElementAt() method:

dict.ElementAt(index).Value

However, this is slower than in the list case.

In either case, you need to check your count. If it is odd, take index = (list.length-1) / 2 ). If it is even, take index1 = list.length/2 AND index2 = list.length/2 - 1 and average the values.