Haz 27 2009

[C#] Use a key string as an array index - Dictionary, SortedDictionary, Hashtable classes

Category: C#Latif @ 12:39

 

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: , , , ,

Comments

1.
simi simi India says:

hi,

First of all. Thanks very much for your useful post.

I just came across your blog and wanted to drop you a note telling you how impressed I was with the information you have posted here.

Please let me introduce you some info related to this post and I hope that it is useful for .Net community.

There is a good C# resource site, Have alook

http://www.csharptalk.com/2009/09/c-array.html
www.csharptalk.com/2009/10/creating-arrays.html

simi

2.
o sullivan coat bb dakota o sullivan coat bb dakota United Kingdom says:

Thanks for this great post. I have implemented it like your post says.

3.
Clarins Clarins Lebanon says:

Hi
I feel enjoy in your site. Your site look awesome and cool.
Happy blogging and keep writing.

thanks

4.
kvalitetna okna kvalitetna okna Slovenia says:

Splendid blog! Its not often that I comment but I felt you deserve it

5.
Online Tagesgeldvergleich Online Tagesgeldvergleich Germany says:

You address truth issue on that stuff. For me you handled it in a professional way. Hope you  go further in this way, with your brilliant way of writing

6.
Daniel Millions Daniel Millions United States says:

Do have an email system where I can get your blog posts emailed to me?

7.
finding cures for genital warts finding cures for genital warts Slovenia says:

It is used to alleviate skin disease such as warts, moles, skin tags, and solar keratoses.