Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between Dictionary and Hashtable in C#
Hashtable is slower than Dictionary. For strongly-typed collections, the Dictionary collection is faster.
Hashtable
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 −
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();
}
}
}
Output
E001: Tom E098: Amit E110: Jack
Dictionary
Dictionary is a collection of keys and values in C#. Dictionary<TKey, TValue> is included in the System.Collection.Generics namespace.
Example
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);
}
}
Output
Dictionary elements: 7
Advertisements