Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge
  • All
  • C#
  • MVC
  • Web API
  • Azure
  • IIS
  • JavaScript
  • Angular
  • Node.js
  • Java
  • Python
  • SQL Server
  • SEO
  • Entrepreneur
  • Productivity

Difference between Hashtable and Dictionary in C#

In C#, Hashtables and Dictionaries are two commonly used collection type for storing and retrieving key-value pairs.

The following example demonstrates creating a Hashtable and adding elements.

Example: Create and Add Elements
Hashtable numberNames = new Hashtable();
numberNames.Add(1,"One"); //adding a key/value using the Add() method
numberNames.Add(2,"Two");
numberNames.Add(3,"Three");

//The following throws run-time exception: key already added.
//numberNames.Add(3, "Three"); 

foreach(DictionaryEntry de in numberNames)
        Console.WriteLine("Key: {0}, Value: {1}", de.Key, de.Value);
Try it

A Dictionary can be created by passing the type of keys and values it can store.

Example: Create Dictionary and Add Elements
IDictionary<int, string> numberNames = new Dictionary<int, string>();
numberNames.Add(1,"One"); //adding a key/value using the Add() method
numberNames.Add(2,"Two");
numberNames.Add(3,"Three");

//The following throws run-time exception: key already added.
//numberNames.Add(3, "Three"); 

foreach(KeyValuePair<int, string> kvp in numberNames)
        Console.WriteLine("Key: {0}, Value: {1}", kvp.Key, kvp.Value);
Try it

Hashtable vs Dictionary

The following table lists the differences between Hashtable and Dictionary in C#.

HashtableDictionary
Hashtable is included in the System.Collections namespace.Dictionary is included in the System.Collections.Generic namespace.
Hashtable is a loosely typed (non-generic) collection, this means it stores key-value pairs of any data types.Dictionary is a generic collection. So it can store key-value pairs of specific data types.
Hashtable is thread safe. Only public static members are thread safe in Dictionary.
Hashtable returns null if we try to find a key which does not exist.Dictionary throws an exception if we try to find a key which does not exist.
Data retrieval is slower than the dictionary collection because of boxing-unboxing.Data retrieval is faster than Hashtable because it is a type safe so no need of boxing-unboxing.

It is recommended to use the Dictionary than the Hashtable collection in C#.

Visit Hashtable or Dictionary in the C# tutorials section for more information.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.