As stated earlier, it??™s the generic replacement for Hashtable
and the related classes, such as StringDictionary. There aren??™t many features present
in Dictionary<,> that aren??™t in Hashtable, although this is partly because the ability to
specify a comparison in the form of an IEqualityComparer was added to Hashtable in
.NET 2.0. This allows for things like case-insensitive comparisons of strings without
using a separate type of dictionary. IEqualityComparer and its generic equivalent,
IEqualityComparer
, have both Equals and GetHashCode. Prior to .NET 2.0 these
were split into IComparer (which had to give an ordering, not just test for equality) and
IHashCodeProvider. This separation was awkward, hence the move to IEquality-
Comparer for 2.0. Dictionary<,> exposes its IEqualityComparer in the public
Comparer property.
The most important difference between Dictionary and Hashtable (beyond the
normal benefits of generics) is their behavior when asked to fetch the value associated
with a key that they don??™t know about. When presented with a key that isn??™t in the
map, the indexer of Hashtable will just return null. By contrast, Dictionary<,> will
throw a KeyNotFoundException. Both of them support the ContainsKey method to
tell beforehand whether a given key is present. Dictionary<,> also provides
TryGetValue, which retrieves the value if a suitable entry is present, storing it in the
output parameter and returning true.
Pages:
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215