tags:

views:

51

answers:

3

I`m not really sure when I should use a dictionary in my applications.

Everytime I code a switch case that returns a single value, for example, should I strive for a dic?

Thanks

+1  A: 

Think of the use of a traditional dictionary. You have a word and its definition. You should use Dictionary objects in your code when you need the Key to have meaning. If the value of the Key is not important then you should look at some other data structure, like a Linked List or even just an Array.

Covar
A: 

Interestingly enough, I would argue that in an important sense a traditional dictionary is a really big switch statement. There are a static set of words being defined.

A dictionary instance, on the other hand, is most useful when your set of keys/words is either (a) not static and/or (b) not known at compile time. This can occur if you are pulling groups of key/value pairs out of a database or performing calculations that have some type of grouping where the grouping is dynamic, such as counting the number of each make/model of a company's employees' cars.

Mark
+2  A: 

A dictionary is useful whenever you have a natural key->value relationship. This is the case in a language dictionary (key:one word value:List of translations), or for a document index (key:title, value:text body).

If you find yourself coding a switch statement where each case is only responsible for returning a single value you have such a natural key->value relationship and should use a dictionary.

Johannes Rudolph