Hi,
I will try to explain the differences between Dictionary, SortedDictionary, Hashtable and
at the same time,we will answer how to use a key string as an array index?
Namespace: System.Collections.Generic
Dictionary(TKey, TValue) Class
SortedDictionary (TKey, TValue) Class
Namespace: System.Collections
Hashtable
Code with explanation:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace genericTypes2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//System.Collections.Generic.Dictionary
Dictionary<string, string> dictionaryNew = new Dictionary<string, string>();
//Use Add() to assign value
dictionaryNew.Add("company", "TRIODOR");
dictionaryNew.Add("grup", "MD INFO");
dictionaryNew.Add("latif", "ozturk");
dictionaryNew.Add("recep", "guzel");
dictionaryNew.Add("talha", "turan");
dictionaryNew.Add("pinar", "alasli");
dictionaryNew.Add("elif", "onder");
//You can assign value like that as well
dictionaryNew["volkan"] = "cakar";
dictionaryNew["bugra"] = "postaci";
dictionaryNew["Erkan"] = "BALABAN";
listBox7.Items.Add("DICTIONARY <>");
listBox7.Items.Add(" ");
//to list dictionaryNew in the ListBox - use KeyValuePair
foreach (KeyValuePair<string, string> obj in dictionaryNew)
{
listBox7.Items.Add(obj.Key + " " + obj.Value);
//other way of getting value
//listBox7.Items.Add(dictionaryNew["" + obj.Key + ""]);
}
//System.Collections.Generic.SortedDictionary
SortedDictionary<string, string> dictionaryNewSorted = new SortedDictionary<string, string>();
//Use Add() to assign value
dictionaryNewSorted.Add("company", "TRIODOR");
dictionaryNewSorted.Add("grup", "MD INFO");
dictionaryNewSorted.Add("latif", "ozturk");
dictionaryNewSorted.Add("recep", "guzel");
dictionaryNewSorted.Add("talha", "turan");
dictionaryNewSorted.Add("pinar", "alasli");
dictionaryNewSorted.Add("elif", "onder");
//You can assign value like that as well
dictionaryNewSorted["volkan"] = "cakar";
dictionaryNewSorted["bugra"] = "postaci";
dictionaryNewSorted["Erkan"] = "BALABAN";
listBox8.Items.Add("SORTED DICTIONARY<>");
listBox8.Items.Add(" ");
//to list dictionaryNewSorted in the ListBox - use KeyValuePair
foreach (KeyValuePair<string, string> obj in dictionaryNewSorted)
{
listBox8.Items.Add(obj.Key + " " + obj.Value);
}
//System.Collections.Hashtable
Hashtable hashTableNew = new Hashtable();
//Use Add() to assign value
hashTableNew.Add("company", "TRIODOR");
hashTableNew.Add("grup", "MD INFO");
hashTableNew.Add("latif", "ozturk");
hashTableNew.Add("recep", "guzel");
hashTableNew.Add("talha", "turan");
hashTableNew.Add("pinar", "alasli");
hashTableNew.Add("elif", "onder");
//You can assign value like that as well
hashTableNew["volkan"] = "cakar";
hashTableNew["bugra"] = "postaci";
hashTableNew["Erkan"] = "BALABAN";
listBox9.Items.Add("HASH TABLE<>");
listBox9.Items.Add(" ");
//to list hashTableNew in the ListBox - use DictionaryEntry
foreach (DictionaryEntry obj in hashTableNew)
{
listBox9.Items.Add(obj.Key + " " + obj.Value);
}
}
}
}

It is easy to see the differences between Dictionary, SortedDictionary, Hashtable in the picture above.
Dictionary -> uses FIFO
SortedDictionary -> sorting by array index
Hashtable -> sorting by hash algorithm
And also don’t forget that Dictionary is faster than others.
See you...
Tags: dictionary, sorteddictionary, hashtable, c#, system.collections