Javatpoint Logo
Javatpoint Logo

Difference between Hashtable and Dictionary in C#

In this article, we will discuss the difference between the Hashtable and Dictionary in C#. But before discussing their differences, we must know about the Hashtable and Dictionary in C# with their examples.

Introduction of Hashtable:

A collection class called Hashtable in C# that is used to hold key/value pairs. It's included in the System.namespace for collections.

Overview of how it works:

  1. Key/Value Pair: Every entry in a hashtable consists of a separate key and its matching information. Values in the hashtable can be replicated, but identifiers need to be distinct.
  2. Hashing: Hashtable maps keys to their associated values internally using a hashing technique. As a consequence, values may be efficiently retrieved using their identification numbers.
  3. Quick Lookup: Hashtables offer quick lookup times for values based on their keys due to hashing. The average length of time required to get an element from a hashtable generally remains constant.
  4. Dynamic Sizing: A hashtable constantly changes size to make room for extra items. This helps preserve efficient performance even when the Hashtable's element count fluctuates.
  5. No Order Guarantee: The order of all the components in a hashtable is not guaranteed, in contrast to several other collection types in C#, such as list and dictionaries. The sequence in which elements are kept in the hashtable is not fixed.

Example:

Let us take an example to illustrate how to use a hashtable in C#:

Output:

Value of 'apple': 10
Value of 'banana': 20
Key: apple, Value: 10
Key: orange, Value: 40

Explanation:

In this example, the given C# code shows how to use the System's Hashtable collection class.namespace for collections. A new Hashtable instance called "hashtable" is created inside the Main function. After that, the Add operation is used to add key/value pairs to this hashtable. As virtually every key has a matching value, values can potentially be efficiently retrieved using just the matching keys.

The code shows how to access values from the hashtable using their corresponding keys after adding the pairs of keys and values. Square brackets and the key name are used to retrieve the values immediately.

After that, the code changes the value linked to the key "orange" by providing it with a new value. Moreover, the entry for the key "banana" is eliminated from the hashtable by employing the delete method to delete a key/value combination.

Lastly, the code uses a foreach loop to traverse over the remaining key/value pairs in the hashtable. Each DictionaryEntry object represents an entry in the hashtable with the key and value included. The hashtable's contents following the adjustments are displayed on the console by printing the key and value of each item.

Introduction Dictionary in C#:

A Dictionary is a generic collection class in C# that facilitates quick lookup, insertion, and deletion decisions by storing key/value pairs. It's included in the System.Collections namespace. Here is a summary of its salient characteristics:

  1. Key/Value Pair: Every element within a dictionary has a distinct key and a corresponding value, just like in a hashtable. Variables can be replicated, but keys in the Dictionary must be distinct.
  2. Firmly Typed: In contrast to Hashtable, the dictionary is firmly typed, which holds objects and necessitates boxing and unpacking. It implies that type correctness is provided and boxing/unboxing operations cannot be performed because we provide the types for each of the keys and values at the time we declare a Dictionary instance.
  3. Type Safety and Performance: Dictionary operates better than Hashtable due to its strong typing, particularly when handling different types of values and eliminating pointless type conversions.
  4. Quick Lookup: Dictionary employs hashing internally, such as Hashtable, which provides quick lookup speeds for values depending on their keys. The average time complexity for retrieving an element from a dictionary is usually constant.
  5. No Order Guarantee: Dictionary, like Hashtable, provides no promises about the arrangement of its components.
  6. Dictionary support for Language Integrated Query (LINQ): LINQ enables us to work with the key/value pairs that are stored in the Dictionary by doing projections, queries, and transformations.

Example:

Let us take an example to illustrate how to use a dictionary in C#:

Output:

Value of 'apple': 10
Value of 'banana': 20
Key: apple, Value: 10
Key: orange, Value: 40

Explanation:

The dictionary is demonstrated in the accompanying C# code, which is a collection type for storing key/value pairs. A new Dictionary object called a dictionary is declared in the code and contains values in integers and string keys. After that, pairs of keys and values that indicate the number of fruits are included in this dictionary. The code shows how to change a value linked to a key and access values using their corresponding keys. It also shows how to take a key/value combination out of the dictionary. Lastly, the source code prints each key/value combination onto the console as iterates over the list of words. The essential C# actions of adding, reading, changing, and deleting items within a dictionary are demonstrated in this example.

Main differences between Hashtable and Dictionary in C#

Difference between Hashtable and Dictionary in C#

There are several main differences between Hashtable and Dictionary in C#. Some main Differences between hashtable and dictionary are as follows:

Features Hashtable Dictionary
Namespace: The System contains the hashtable namespace for collections. There is a dictionary in the system. The namespace Collections.
Type Safety: A hashtable lacks type safety. It's designed to be boxed/unboxed and store items.
The dictionary has type safety. It serves as a generic collection class that lets us to set the kinds of values and keys. Technology improves performance and does away with having to pay for boxes and unpacking, especially when dealing with bargain varieties.
Achievement: When it comes to value types specifically, Hashtable might perform slightly less well than Dictionary due to boxing/unboxing and an absence of type safety. Dictionary's type safety and avoidance of boxing and unpacking procedures result in improved efficiency, especially when dealing with value types.
Good Typing: Compile-time type verification for both values and keys cannot be provided by hashtables. The dictionary ensures type safety at build time by offering compile-time type checking to feed keys along with values.
Compatibility: Any kind of information may be represented in a hashtable as keys along with values. When declaring a list of characters, the key and value types are required to be specified explicitly.
Enumeration: Boxing and unpacking techniques may occur when the for each operator loop is used to iterate along a hashtable. The dictionary's safety feature allows for greater effectiveness than enumerating over its items.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA