In this article I’m explaining about Hashtable VS Dictionary in C#.
if you learn more about Create and Manage Thread read my previous post Creating, Managing and Destroying Threads in C#
Hashtable:
The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. It uses the key to access the elements in the collection.
A hash table is used when you need to access elements by using key, and you can identify a useful key value. Each item in the hash table has a key/value pair. The key is used to access the items in the collection.
Example
using System;
using System.Collections;
namespace ExHashtable
{
classProgram
{
staticHashtable GetHashtable()
{
Hashtable hashtable = newHashtable();
hashtable.Add("Kamlakar", "jaisalmer");
hashtable.Add("Rohit", "Allahabad");
hashtable.Add("Pawan", "Goa");
return hashtable;
}
publicstaticvoid Main()
{
Hashtable hashtable = GetHashtable();
string add1 = (string)hashtable["Kamlakar"];
Console.WriteLine(add1);
string add2 = (string)hashtable["Rohit"];
Console.WriteLine(add2);
string add3 = (string)hashtable["Pawan"];
Console.WriteLine(add3);
}
}
}
Output
Dictionary:
A dictionary is used where fast lookups are critical. The Dictionary type provides fast lookups with keys to get values. With it we use keys and values of any type, including ints and strings. Dictionary requires a special syntax form.
Dictionary is used when we have many different elements. We specify its key type and its value type. It provides good performance.
Creating a Dictionary
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.
using System.Collections.Generic;
The Dictionary class constructor takes two parameters (generic type), first for the type of the key and second for the type of the value. The following code snippet creates a Dictionary where keys are strings and values are short.
Dictionary<string, Int16> AuthorList = new Dictionary<string, Int16>();z
Example
using System;
using System.Collections;
namespace ExHashtable
{
classProgram
{
staticvoid Main()
{
Dictionary<string, Int16> EmployeeList = newDictionary<string, Int16>();
EmployeeList.Add("Rohit Kesharwani",26);
EmployeeList.Add("Sumit Kesarwani",35);
EmployeeList.Add("Pawan Shukla",24);
EmployeeList.Add("Sunil Kumar", 25);
EmployeeList.Add("Kamlakar Singh",22);
EmployeeList.Add("Manoj Pandey",23);
EmployeeList.Add("Anchal Kesahrwani", 24);
// Read all data
Console.WriteLine("Employee List");
foreach (KeyValuePair<string, Int16> employee in EmployeeList)
{
Console.WriteLine("Key = {0},Value = {1}", employee.Key,employee.Value);
}
Console.ReadKey();
}
}
}
Output
Properties
Count: The count property gets the number of key/value pairs in dictionary. The fallowing code display the number of items in dictionary.
Console.WriteLine("Count: {0}",EmployeeList.Count);
Output
Count: 7
Differences between Hashtable and Dictionary
Dictionary
- It returns error if we try to find a key which does not exist.
- It is faster than a Hashtable because there is no boxing and unboxing.
- Only public static members are thread safe.
- Dictionary is a generic type which means we can use it with any data type.
Hashtable
- It returns null if we try to find a key which does not exist.
- It is slower than dictionary because it requires boxing and unboxing.
- All the members in a Hashtable are thread safe,
- Hashtable is not a generic type,
in my next post, i'll explain about Backbone.js
Anonymous User
06-Oct-2014Anonymous User
23-Sep-2014