Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't a Dictionary access nonexistent keys like a Hashtable does?

If I'm using a Hashtable, I can write code like this:

object item = hashtable[key] ?? default_value;

That works whether or not key appears in the Hashtable.

I can't do that with a Dictionary<TKey. TValue>. If the key's not present in the dictionary, that will throw a KeyNotFoundException. So I have to write code like this:

MyClass item;
if (!(dict.TryGetValue(key, out item))
{
   item = default_value;
}

I'm wondering why this is. Dictionary<TKey, TValue> is just a wrapper around Hashtable. Why has this restriction been added to it?

Edit:

For another perspective on PopCatalin's answer (see below), the code I'd written above won't work if the dictionary's values are of a value type. If I'm using a Dictionary<int, int>, then the code I would like to use looks like this:

int i = dict[key] ?? default_value;

And that won't compile, because dict[key] isn't a nullable or reference type.

like image 649
Robert Rossney Avatar asked Dec 30 '08 01:12

Robert Rossney


People also ask

What is the difference between Hashtable and Dictionary?

In Hashtable, you can store key/value pairs of the same type or of the different type. In Dictionary, you can store key/value pairs of same type. In Hashtable, there is no need to specify the type of the key and value. In Dictionary, you must specify the type of key and value.

Why dictionary is faster than hash table?

Dictionary is a generic type and returns an error if you try to find a key which is not there. The Dictionary collection is faster than Hashtable because there is no boxing and unboxing.

What is the difference between Dictionary and Hashtable in C#?

HashTable is the non-generic type of collection which is used to store data in key/value pair and is defined in System. Collections name space. On other hand Dictionary is a generic type collection defined under System.

Is Dictionary hashable in C#?

Dictionary is NOT implemented as a HashTable, but it is implemented following the concept of a hash table. The implementation is unrelated to the HashTable class because of the use of Generics, although internally Microsoft could have used the same code and replaced the symbols of type Object with TKey and TValue. In .


2 Answers

The difference between a Dictionary<T> and a Hashtable is that a Dictionary<T> is a generic type that can specialize to store value types along reference types.

The hashtable can only store reference types (a Object passed by reference) and only value types that are boxed (also passed by reference).

When a dictionary is specialized with value types, it must return those values 'by value' not by reference.So, therefore, a Dictionary<T> can't return null, as null is not a valid value for value types.

like image 162
Pop Catalin Avatar answered Oct 24 '22 16:10

Pop Catalin


There is one misconception in your post. Dictionary is not a wrapper around Hashtable. It's a completely different implementation.

The reason why this change was made is mainly justified by one assertion: Null is a valid value for a hashtable. Without this change it's not possible to distinguish between a non-exsistent key and a value key with a null value using the [] method of access. Dictionary clears this up.

like image 29
JaredPar Avatar answered Oct 24 '22 16:10

JaredPar