- Home /
Hashtable(or generic dictionary) vs Enum - when do I use which?
Both hashtable (or its generic form - dictionary) and enum seems like "reference tables" that can act similarly. I've only learned about them briefly through tutorials and haven't had much practical experience with them, and I'm just wondering, in what situations would I use one over the other?
Answer by dmg0600 · Sep 20, 2014 at 09:16 AM
Enums are usually used to store indexes or types.
If you have, let's say a script for menu behaviour in which you have to make some things differently depending in which menu you are at the moment, you could use an enum to store the menu types. That way you can have a variable to see which type your menu is: public enum MenuTypes { Main, Pause, Finish, Options }
public MenuTypes myType = MenuTypes.Main;
Dictionaries are used to stored objects or values linked to an index. This is useful to store for example player scores.
Dictionary<string, int> playerScores = new Dictionary<string, int>();
playerScores.Add("John", 25);
playerScores.Add("Anne", 38);
That way I have created a dictionary and stored scores for John and Anne. If a would like to know the score of one player I would go like this: int scoreOfAnne = playerScores["Anne"];
Summary: enums are for enumerate things so you could use to identify them later. Dictionaries are used to store values (objects or variables) based on a key.