Dictionary implementation of SortedList Class in .NET Framework

SortedList class resembles simple list with methods quite similar to those of simple list class.

SortedList mySortedList = new SortedList();
mySortedList["First"] = "1st";
mySortedList["Second"] = "2nd";
mySortedList["Third"] = "3rd";
foreach (DictionaryEntry entry in mySortedList)
    Console.WriteLine("{0} = {1}", entry.Key, entry.Value);

The main difference between SortedList and Hashtable is that SortedList supports indexes and has additional property and methods to those of IDictionary interface.



Capacity Gets or sets the number of items of the collection



ContainsKey Contains a specific key
ContainsValue Contains a specific value
GetByIndex Gets the value at the index
GetKey Gets the key at the index
GetKeyList Gets a list of the keys
GetValueList Gets a list of values
IndexOfKey Index of a key in the collection
IndexOfValue Gets the index of the first occurrence
RemoveAt Removes a value in the collection
SetByIndex Replaces a value at index
TrimToSize Frees unused capacity

Since this is Sorted List the indexes are being rearranged every time you add or remove value from the list.