I am finding that as you learn to use and implement more sophisticated programming techniques the easier it becomes to created what many would call “complex algorithms”. I believe this, just think about what alternative you have to a Dictionary. I mean other than the similar 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 because I did not need the data to be in any specific order, accessing it randomly 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 Dictionary. Instantiate it, load it with data and read the data from it. Sometimes you want check if a value exists within the dictionary, so I will cover that too.
Instantiating a Dictionary is performed like this:
[sourcecode language="csharp" padlinenumbers="true" autolinks="false" gutter="false" toolbar="false"] public static Dictionary<string, int> theDictionary = new Dictionary<string, int>(); [/sourcecode]
I use the static keyword because in this instance I use it like a global variable from within one of my classes.
Loading the data is performed like this:
[sourcecode language="csharp"] theDictionary.Add("KEY1", 10); theDictionary.Add("KEY2", 123); theDictionary.Add("KEY3", 16); [/sourcecode]
Getting the value from a key is as simple as:
[sourcecode language="csharp"] int keyValue = theDictionary["KEY2"]; [/sourcecode]
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.
[sourcecode language="csharp" autolinks="false" gutter="false" toolbar="false"] bool exists = theDictionary.ContainsKey(("KEY1"); [/sourcecode]
In the above line, exists would be true and you would need to take appropriate action based on your requirements.