Jun 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.

8.
stock quotes stock quotes United States says:

Have you considered adding some sort of bookmarking buttons or links to your blog posts?

9.
personal loans for people with bad credit personal loans for people with bad credit United States says:

Do you mind if I quote you on my blog if I link back to this page?

10.
Leisha Dudding Leisha Dudding United States says:

Though I would've loved it much more if you added a relevant video or at least pictures to back up the explanation, I still thought that your write-up quite helpful. It's usually hard to make a complicated matter seem very easy. I enjoy your weblog and will sign up to your feed so I will not miss anything. Fantastic content

11.
Used Computers Used Computers Canada says:

Well, I have been reading your blog posts daily and the reason I come on your blog frequently is its compelling content… Regards… http://www.pctechoutlet.com

12.
Kartenlegen per Mail Kartenlegen per Mail Germany says:

Thanks for the time and effort you put into your blog and detailed information you offer! I will bookmark your blog now. Thumbs up!

13.
List Of Penny Stocks List Of Penny Stocks Portugal says:

I am a frequent reader of your blog and just wanted to inform you that I really like your blog.

14.
Legal Sounds Legal Sounds United Kingdom says:

I can see that you are an expert in this area. I am launching a website soon, and your information will be very useful for me.. Thanks for all your help and wishing you all the success in your business.

15.
privat Krankenversicherung privat Krankenversicherung Austria says:

You're an extremely sensible person!

16.
age smart age smart United Kingdom says:

Sounds like a great idea. But can you put it into action is the question.

17.
Erlinda Aggas Erlinda Aggas United States says:

Very good observations. Maybe adding some pictures will make the blog post more interesting.

18.
black halo knockoff black halo knockoff United Kingdom says:

Great post, it still amazes me as to the ammount of info that is available on the net.

19.
Laronda Ruyz Laronda Ruyz United States says:

This can be the foremost useful article I’ve scan all year. GREAT stuff, many thanks!

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading