Hashtable is slower than Dictionary. For strongly-typed collections, the Dictionary collection is faster.
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.
Let us see an example −
using System; using System.Collections; namespace Demo { class Program { static void Main(string[] args) { Hashtable ht = new Hashtable(); ht.Add("E001", "Tom"); ht.Add("E098", "Amit"); ht.Add("E110", "Jack"); ICollection key = ht.Keys; foreach (string k in key) { Console.WriteLine(k + ": " + ht[k]); } Console.ReadKey(); } } }
E001: Tom E098: Amit E110: Jack
Dictionary is a collection of keys and values in C#. Dictionary<TKey, TValue> is included in the System.Collection.Generics namespace.
using System; using System.Collections.Generic; public class Demo { public static void Main() { IDictionary<int, int> dict = new Dictionary<int, int>(); dict.Add(1,234); dict.Add(2,489); dict.Add(3,599); dict.Add(4,798); dict.Add(5,810); dict.Add(6,897); dict.Add(7,909); Console.WriteLine("Dictionary elements: "+dict.Count); } }
Dictionary elements: 7