Wednesday, September 30, 2009

Let see how to use Dictionary/SortedDictionary classes from System.Collections.Generic(C#)

Hi Readers,

Thank you again for your emails.I`ve been busy lately doing some code for my open source software i will be publishing next month.Again thank you for sending emails.I received 52 yesterday.

Today i will explain to you how to use these 2 classes Dictionary/SortedDictionary.


Step1:we have to import System.Collections.Generic:

using System.Collections.Generic;

Step2: we have to declare Dictionary object with new keyword

Dictionary dic = new Dictionary();

You have to remember that we are dealing with key/pair schema.

Step3:Now let`s add some items with Add(key,value) method

dic.Add(2000, "Talley");
dic.Add(10, "John");
dic.Add(200, "Sam");

Step4:Let`s use ContainsKey method to find values

if (dic.ContainsKey(2000))
string st = dic[10];
Console.WriteLine("No Match!");

You can also use ContainsValue method to query the Dictionay object.There are methods and
properties of Dictionay object you may have to take a look.
SortedDictionay and Dictonary classes have almost the same methods and properties except
SortedDictionay accepts ICompareras parameter like this:

SortedDictionary<string,int> sd=new SortedDictionary<string,int>(IComparer<string>();


In a SortedDictionary elements must be sorted when they are added, making insertion times slower.

Remember you could also link table columns to these Dictionay objects.

Thank you for stopping by.See you next time.
If you have any question concerning this post please feel free to send me email at

Software Developer,ISV

No comments:

About Me

My photo
Raleigh, NC, United States
I am a software developer based in Raleigh,NC,USA.I design softwares and systems ;i also do consulting for companies worldwide.I program with these languages:VB.NET 2003/2005/2008;C#;Java(fun),SQL(200,2005,2008);ASP.NET 2.0/3.5;ASP.NET AJAX;ASP.NET MVC;JavaScript;JQuery;Windows Workflow Foundation;Web Services.I have 4 years + in programming.