Hi all,
What is the difference between hash table and dictionary in C#?
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
VulpesRank #55 · 29.2K reputationPosted Nov 21, 2012, 11:39 PM EST
This means that Dictionary stores the key/value pairs internally as their actual types. However, Hashtable stores them as type System.Object.
Consequently, when using Hashtable, you always need to cast the values from object to their actual type in order to be able to do anything useful with them.
Worse still, if the key or value is a value type (such as int, double, bool etc), then it needs to be 'boxed' on the heap - a relatively slow operation - so that it can be stored as System.Object. This means that Hashtable is slower than Dictionary where value types are concerned.
Satyapriya NayakRank #47 · 31.5K reputationPosted Nov 22, 2012, 12:54 AM EST
If you want to find something in a hashtable you dont have to go through each value in hashtable, instead search for key values and is faster.
Program that uses Hashtable [C#]
using System;
using System.Collections;
class Program
{
static void Main()
{
Hashtable hashtable = new Hashtable();
hashtable[1] = "One";
hashtable[2] = "Two";
hashtable[13] = "Thirteen";
foreach (DictionaryEntry entry in hashtable)
{
Console.WriteLine("{0}, {1}", entry.Key, entry.Value);
}
}
}
Dictionary
A Dictionary class represents a dictionary in C# that is used to represent a collection of keys and values pair of data. This article demonstrates how to use a dictionary in C#.
The Dictionary class is a generic class and can store any data types. This class is defined in the System.Collections.Generic namespace. Before you use a Dictionary class in your code, you must import the System.Collections.Generic namespace using the following line.
C#
using System.Collections.Generic;
VB.NET
Imports System.Collections.Generic
As other collection classes, the Dictionary class has Add, Remove, RemoveAt and other collection methods. The Key and Value property are used to extract a key and a value from an item in a Dictionary.
The following code snippet creates a Dictionary, adds some data to it and reads data back.
VB.NET Code:
Imports System.Collections.Generic
Module Module1
Sub Main()
Dim AuthorList As New Dictionary(Of String, Int16)
AuthorList.Add("Mahesh Chand", 35)
AuthorList.Add("Mike Gold", 25)
AuthorList.Add("Praveen Kumar", 29)
AuthorList.Add("Raj Beniwal", 21)
AuthorList.Add("Dinesh Beniwal", 84)
' Read all data
Console.WriteLine("Authors List")
For Each author As KeyValuePair(Of String, Int16) In AuthorList
Console.WriteLine("Key = {0}, Value = {1}", _
author.Key, author.Value)
Next author
Console.ReadKey()
End Sub
End Module
C# Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Generic;
namespace DictionarySample
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, Int16> AuthorList = new Dictionary<string, Int16>();
AuthorList.Add("Mahesh Chand", 35);
AuthorList.Add("Mike Gold", 25);
AuthorList.Add("Praveen Kumar", 29);
AuthorList.Add("Raj Beniwal", 21);
AuthorList.Add("Dinesh Beniwal", 84);
// Read all data
Console.WriteLine("Authors List");
foreach( KeyValuePair<string, Int16> author in AuthorList )
{
Console.WriteLine("Key = {0}, Value = {1}",
author.Key, author.Value);
}
Console.ReadKey();
}
}
}
Please refer the below links
http://www.dotnetperls.com/hashtable
http://www.c-sharpcorner.com/UploadFile/mahesh/dictionary-in-C-Sharp/
http://www.dotnetperls.com/dictionary
http://www.c-sharpcorner.com/uploadfile/niradhip/dictionary-in-C-Sharp/
winston Gelmer2 reputationPosted Aug 1, 2014, 12:25 AM EDT
Declaration:
Hashtable numbers = new Hashtable();
Dictionary dictionary = new Dictionary();
More about...http://net-informations.com/faq/general/dictionary.htm
http://csharp.net-informations.com/collection/dictionary.htm
Winston
Abhishek JaiswalRank #126 · 17.5K reputationPosted Aug 1, 2014, 12:56 AM EDT
check this
http://www.c-sharpcorner.com/UploadFile/2072a9/dictionaries-and-hash-table-in-C-Sharp496/
:)