Why is Dictionary preferred over Hashtable in C#?

后端 未结 19 1237
长情又很酷
长情又很酷 2020-11-22 05:53

In most programming languages, dictionaries are preferred over hashtables. What are the reasons behind that?

相关标签:
19条回答
  • 2020-11-22 06:37

    According to what I see by using .NET Reflector:

    [Serializable, ComVisible(true)]
    public abstract class DictionaryBase : IDictionary, ICollection, IEnumerable
    {
        // Fields
        private Hashtable hashtable;
    
        // Methods
        protected DictionaryBase();
        public void Clear();
    .
    .
    .
    }
    Take note of these lines
    // Fields
    private Hashtable hashtable;
    

    So we can be sure that DictionaryBase uses a HashTable internally.

    0 讨论(0)
  • 2020-11-22 06:39

    Notice that MSDN says: "Dictionary<(Of <(TKey, TValue>)>) class is implemented as a hash table", not "Dictionary<(Of <(TKey, TValue>)>) class is implemented as a HashTable"

    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 .NET 1.0 Generics did not exist; this is where the HashTable and ArrayList originally began.

    0 讨论(0)
  • 2020-11-22 06:41

    Dictionary <<<>>> Hashtable differences:

    • Generic <<<>>> Non-Generic
    • Needs own thread synchronization <<<>>> Offers thread safe version through Synchronized() method
    • Enumerated item: KeyValuePair <<<>>> Enumerated item: DictionaryEntry
    • Newer (> .NET 2.0) <<<>>> Older (since .NET 1.0)
    • is in System.Collections.Generic <<<>>> is in System.Collections
    • Request to non-existing key throws exception <<<>>> Request to non-existing key returns null
    • potentially a bit faster for value types <<<>>> bit slower (needs boxing/unboxing) for value types

    Dictionary / Hashtable similarities:

    • Both are internally hashtables == fast access to many-item data according to key
    • Both need immutable and unique keys
    • Keys of both need own GetHashCode() method

    Similar .NET collections (candidates to use instead of Dictionary and Hashtable):

    • ConcurrentDictionary - thread safe (can be safely accessed from several threads concurrently)
    • HybridDictionary - optimized performance (for few items and also for many items)
    • OrderedDictionary - values can be accessed via int index (by order in which items were added)
    • SortedDictionary - items automatically sorted
    • StringDictionary - strongly typed and optimized for strings
    0 讨论(0)
  • 2020-11-22 06:44

    Since .NET Framework 3.5 there is also a HashSet<T> which provides all the pros of the Dictionary<TKey, TValue> if you need only the keys and no values.

    So if you use a Dictionary<MyType, object> and always set the value to null to simulate the type safe hash table you should maybe consider switching to the HashSet<T>.

    0 讨论(0)
  • 2020-11-22 06:44

    One more difference that I can figure out is:

    We can not use Dictionary<KT,VT> (generics) with web services. The reason is no web service standard supports the generics standard.

    0 讨论(0)
  • 2020-11-22 06:45

    In .NET, the difference between Dictionary<,> and HashTable is primarily that the former is a generic type, so you get all the benefits of generics in terms of static type checking (and reduced boxing, but this isn't as big as people tend to think in terms of performance - there is a definite memory cost to boxing, though).

    0 讨论(0)
提交回复
热议问题