Monday 17 December 2012

c sharp programming

Building Dictionaries:
The DictionaryBase Class
and the SortedList Class


A dictionary is a data structure that stores data as a key–value pair. The
DictionaryBase class is used as an abstract class to implement different data
structures that all store data as key–value pairs. These data structures can be
hash tables, linked lists, or some other data structure type. In this chapter,
we examine how to create basic dictionaries and how to use the inherited
methods of the DictionaryBase class.We will use these techniques later when
we explore more specialized data structures.
One example of a dictionary-based data structure is the SortedList. This
class stores key–value pairs in sorted order based on the key. It is an interesting
data structure because you can also access the values stored in the structure
by referring to the value’s index position in the data structure, which makes
the structure behave somewhat like an array.We examine the behavior of the
SortedList class at the end of the chapter.


THE DICTIONARYBASE CLASS
You can think of a dictionary data structure as a computerizedword dictionary.
The word you are looking up is the key, and the de?nition of the word is the
value. The DictionaryBase class is an abstract (MustInherit) class that is used
as a basis for specialized dictionary implementations.
The key–value pairs stored in a dictionary are actually stored as Dictio-
naryEntry objects. The DictionaryEntry structure provides two ?elds, one for
the key and one for the value. The only two properties (or methods) we’re
interested in with this structure are the Key and Value properties. Thesemeth-
ods return the values stored when a key–value pair is entered into a dictionary.

Internally, key–value pairs are stored in a hash table object called Inner-
HashTable.
just view it as an ef?cient data structure for storing key–value pairs.
The DictionaryBase class actually implements an interface from the Sys-
tem.Collections namespace, IDictionary. This interface is actually the basis
for many of the classes we’ll study later in this book, including the ListDic-
tionary class and the Hashtable class.


No comments:

Post a Comment