I usually use the dictionary as a list, but with a different type of key. I like the ability to quickly access individual elements in the dictionary without having to scroll until I find the element with the property right (because the property I'm looking for is in the key).
But there is another possible use of the dictionary. I could just use the key to store property A and Value to store property B without using the special dictionary functionality. For example, I could keep a list of people by simply storing the first name in the key and the last name in the value (let's say for simplicity that there will never be two people with the same name, because I just could not come up with a better example). I would use only this dictionary to iterate over it in the foreach loop and add elements to it (without deleting, sorting, or accessing individual elements). In fact, there is no difference in using List<KeyValuePair<string, string>> from using Dictionary<string, string> (at least not in the example I gave - I know that I could, for example, store several elements with with the same key in the list).
So, to summarize, what should I do when I do not need to use the special functions that the dictionary offers, and just use it to store something that has exactly two properties:
- use
Dictionary<,> - use
List<KeyValuePair<,> - use
List<MyType> with MyType as a custom class that contains two properties and a constructor.
dictionary c #
jalgames
source share