Both non-generic and generic variants of the hash table-related classes do not keep the order of the elements. For this reason, if you need to present data from the collection sorted by keys, you need to sort them prior to presentation. However, you can use another data structure, the sorted dictionary, to solve this problem and keep keys sorted all the time. Therefore, you can easily get the sorted collection whenever necessary.
The sorted dictionary is implemented as the
SortedDictionary generic class, available in the
System.Collections.Generic namespace. You can specify types for keys and values while creating a new instance of the
SortedDictionary class. Moreover, the class contains similar properties and methods to
First of all, you can use the indexer to get access to a particular element within the collection, as shown in the following line of code:
string value = dictionary["key"];
You should ensure that the element exists in the collection. Otherwise...