C# Hashtable vs Dictionary Last Updated : 31 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report In C# both Hashtable and Dictionary are used to store key-value pairs. Understanding the difference between Hashtable and Dictionary plays a very important role in choosing the right data structure for our C# applications. The main difference between Hashtable and Dictionary is:Hashtable: This is an older, non-generic collection that treats everything as objects. This means it doesn’t enforce any type of safety, which could lead to errors at runtime if the wrong types are used.Dictionary<TKey, TValue>: This is a modern, generic collection that works with specific types. This provides type safety, and better performance, and catches errors during compilation, making it a safer and faster option.Note: Dictionary<TKey, TValue> is the preferred choice for most C# codes today because it’s more reliable and easier to work with.Hashtable vs Dictionary FeaturesHashtableDictionaryGeneric/Non-GenericHashtable is Non-generic.Dictionary is generic.NamespaceIt is defined under System.CollectionsIt is defined under System.Collections.GenericsType SafetyIt does not enforce type safety.It enforces type safety.Boxing/UnboxingIt is slower because boxing/unboxing of data.It is faster because there is no boxing/unboxing.Accessing Missing KeysIt returns null for missing keys.It returns KeyNotFoundException for missing keys.Thread-SafetyIt is thread-safeIt is not thread-safePerformanceIt is slower due to boxing/unboxing.It is faster due to generics and no boxing/unboxing.C# HashtableA Hashtable is a collection of key/value pairs that are arranged based on the hash code of the key. Or in other words, a Hashtable is used to create a collection which uses a hash table for storage. It is the non-generic type of collection which is defined in System.Collections namespace. In Hashtable, key objects must be immutable as long as they are used as keys in the HashtableExample: This example demonstrates how to create a Hashtable in C#, add key-value pairs, and print each key-value pair using a foreach loop. C# // C# program to illustrate a hashtable using System; using System.Collections; class Geeks { // Main method static public void Main() { // Create a hashtable // Using Hashtable class Hashtable ht = new Hashtable(); // Adding key/value pair in the hashtable // Using Add() method ht.Add("1", "Welcome"); ht.Add("2", "to"); ht.Add("3", "GeeksforGeeks"); foreach(DictionaryEntry ele in ht) { Console.WriteLine("Key: {0} and Value: {1} ", ele.Key, ele.Value); } } } OutputKey: 3 and Value: GeeksforGeeks Key: 2 and Value: to Key: 1 and Value: Welcome C# DictionaryIn C#, Dictionary is a generic collection which is generally used to store key/value pairs. Dictionary is defined under System.Collection.Generics namespace. It is dynamic in nature means the size of the dictionary is growing according to the needExample: This example demonstrates how to create a Dictionary in C#, add key-value pairs, and then print each key-value pair using a foreach loop. C# // C# program to illustrate Dictionary using System; using System.Collections.Generic; class Geeks { // Main Method static public void Main() { // Creating a dictionary // using Dictionary<TKey, TValue> class Dictionary<string, string> d = new Dictionary<string, string>(); // Adding key/value pairs in the Dictionary // Using Add() method d.Add("1", "C"); d.Add("2", "C++"); d.Add("3", "C#"); foreach(KeyValuePair<string, string> element in d) { Console.WriteLine("Key: {0} and Value: {1}", element.Key, element.Value); } } } OutputKey: 1 and Value: C Key: 2 and Value: C++ Key: 3 and Value: C# Comment More infoAdvertise with us Next Article C# Hashtable vs Dictionary A ankita_saini Follow Improve Article Tags : Difference Between C# CSharp-Collections-Hashtable CSharp Dictionary Class Similar Reads Difference Between IPv4 and IPv6 IPv4 and IPv6 are two versions of the system that gives devices a unique address on the internet, known as the Internet Protocol (IP). IP is like a set of rules that helps devices send and receive data online. Since the internet is made up of billions of connected devices, each one needs its own spe 7 min read Difference between BFS and DFS Breadth-First Search (BFS) and Depth-First Search (DFS) are two fundamental algorithms used for traversing or searching graphs and trees. This article covers the basic difference between Breadth-First Search and Depth-First Search.Difference between BFS and DFSParametersBFSDFSStands forBFS stands fo 2 min read Differences between TCP and UDP Transmission Control Protocol (TCP) and User Datagram Protocol (UDP) both are protocols of the Transport Layer Protocols. TCP is a connection-oriented protocol whereas UDP is a part of the Internet Protocol suite, referred to as the UDP/IP suite. Unlike TCP, it is an unreliable and connectionless pr 9 min read Differences Between JDK, JRE and JVM Understanding the difference between JDK, JRE, and JVM plays a very important role in understanding how Java works and how each component contributes to the development and execution of Java applications. The main difference between JDK, JRE, and JVM is:JDK: Java Development Kit is a software develo 3 min read Difference Between OSI Model and TCP/IP Model Data communication is a process or act in which we can send or receive data. Understanding the fundamental structures of networking is crucial for anyone working with computer systems and communication. For data communication two models are available, the OSI (Open Systems Interconnection) Model, an 5 min read C# Tutorial C# (pronounced "C-sharp") is a modern, versatile, object-oriented programming language developed by Microsoft in 2000 that runs on the .NET Framework. Whether you're creating Windows applications, diving into Unity game development, or working on enterprise solutions, C# is one of the top choices fo 4 min read Introduction to .NET Framework The .NET Framework is a software development framework developed by Microsoft that provides a runtime environment and a set of libraries and tools for building and running applications on Windows operating systems. The .NET framework is primarily used on Windows, while .NET Core (which evolved into 6 min read Difference Between Method Overloading and Method Overriding in Java Understanding the difference between Method Overloading and Method Overriding in Java plays a very important role in programming. These two are the important concepts that help us to define multiple methods with the same name but different behavior, both of these are used in different situations. Th 6 min read Java Checked vs Unchecked Exceptions In Java, an exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at run time, that disrupts the normal flow of the programâs instructions. In Java, there are two types of exceptions:Checked Exception: These exceptions are checked at compile time, forcing 5 min read Difference between SaaS, PaaS and IaaS Cloud Computing has transformed the way companies access, manage, and expand their IT resources. Among the many cloud services models, IaaS(Infrastructure as a Service), PaaS(Platform as a Service), and SaaS(Software as a Service) are the most popular. Each of these models provides different service 7 min read Like