The Best C# Programmer In The World - Ben Perkins Member Login  | Newsletter | RSS Feeds


 
 
 
TheBestCSharpProgrammerInTheWorld.com 
 
The Best C# Programmer In The World - Ben Perkins
Dictionary in C#  
 
Dictionary in C#
 
Please rate:
 
I am finding that as you learn to use and implement more syphistocated programming techniques the eaiser it becomes to created what many would call "complex algorithms". I beleive this, just think about what alternative you have to a Dictionary. I mean other than the similiar methods found in the System.Collections or Systems.Collections.Generic classes. Configuration files, database selects, registry settings, etc...all of which are not responsive enough. Having the ability to store (Key, Value) pairs in memory, access them and change them at a very responsive rate provides the right solution for many projects. For example, I needed to build an HQL (NHibernate) query dynamically based on the selection from a treeview. The treeveiw could be expanded n levels. Plus I needed to create joins, table aliases and where clauses. I needed to maintain counters, class names and table names during the process.
 
I chose to use a Dictionary versus a SortedList or other becuase I did not need the data to be in any specific order, accessing it randomlly and would not have too many items in it. It seemed to be the best choice versus the other possibilities. That's one thing I can recommend when you are starting you quest to become the best C# programmer in the world. Always look at the alternatives, always have a reason why you made the decision you did and always have reasons why you didn't choose the alternative. It not only helps you improve your scope of understanding but will improve your technical jargon and improve your design skills on future projects.
 
There are only 3 things you really need to do with a Dictiionary. Instantiate it, load it with data and read the data from it. Sometimes you want check if a value exists within the dictiionary, so I will cover that too.
 
Instantiating a Dictionary is performed like this:
 
                            
                            public static Dictionary<string, int> theDictionary = 
                                    new Dictionary<string, int>();
                            
                            
 
I use the static keyword becuase in this instance I use it like a global variable from within one of my classes.
 
Loading the data is performed like this:
 
                            
                            theDictionary.Add("KEY1", 10);
                            theDictionary.Add("KEY2", 123);
                            theDictionary.Add("KEY3", 16);
                            
                            
 
Getting the value from a key is as simple as:
 
                            
                            int keyValue = theDictionary["KEY2"];
                            
                            
 
The value for keyValue would be 123.
 
And finally, to check is a value exists within the Dictionary, you may want to do this before you add a new value, NOTE: that the values stored as the KEY must be unique and you will get an exception if you try to store something with a duplicate key. So it is always good to check before you add or put your add with a try..catch block and handle the exception.
 
                            
                            bool exists = theDictionary.ContainsKey(("KEY1");
                            
                            
 
In the above line, exists would be true and you would need to take appropriate action based on your requirements.
 
 
Feedback / Question
 
Your Name:Your Email:
 
Subject:
 
Feedback/Question:
 
 
 
I had to remove the capability to leave feedback due to this. Will be back soon.
 
 
Comment posted: 5/10/2011 11:48:44 AM by Boss
 
Stands back from the kebyoard in amazement! Thanks!
 
page.Translate()
 
 
blog.Stats()
 
  Posts: 113
  Comments: 86
  Fundamentals: 16
 
my.Book()

 
me.About()
 
 
 
 
 
blog.Archive()
 
2012 May  (4)
2012 April  (5)
2012 March  (4)
2012 February  (4)
2012 January  (5)
2011 December (2)
2011 November (6)
2011 October (7)
2011 September (7)
2011 August (9)
2011 July (9)
2011 June (8)
2011 May (9)
2011 April (7)
2011 March (9)
2011 February (8)
2011 January (8)
2010 December (7)
2010 November (8)
2010 October (4)
 
site.Visits()
 
free counters
 
tag.Cloud()
 
code.Disclaimer()
 
The sample code on this website is provided to illustrate a concept and should not be used in applications or Web sites without proper professional consultation, as it may not illustrate the safest coding practices. I assume no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.
 
   


The Best C# Programmer In The World - Ben Perkins, © 2010, All Rights ReservedContact Ben