Understanding Dictionary Usage with Enum
What is an enum?
In C#, an enum represents a collection of named constants.
By default, enum values start from 0, but it is recommended to explicitly assign = 0 to avoid confusion when linking with arrays or other data structures.
enum PlayerState
{
Idle = 0,
Run,
Jump
}
In this example, Idle has a value of 0, Run is 1, and Jump is 2.
If you do not specify a value, they automatically increment by 1.
How to convert a string to an enum value (Enum.TryParse)
To convert a string to an enum value, use the Enum.TryParse<T>() method.
For example, if you have the string "Run", you can convert it to PlayerState.Run like this:
Enum.TryParse<PlayerState>("Run", out PlayerState state);
Debug.Log(state);
// Output: Run
If the conversion is successful, state will hold the PlayerState.Run value.
If it fails, it returns false, allowing safe error handling.
Comparison with int.TryParse
int.TryParse("123", out int number) does not use generics because int is a single type.
However, there are many different enums like PlayerState, MonsterState, and WeaponState.
Therefore, Enum.TryParse<EnumType>() requires you to specify the target enum type with a generic parameter.
Precautions when using enums with arrays
When using enums with arrays, be cautious. Arrays always start from index 0, but if an enum does not start from 0, indexing mismatches can occur.
enum PlayerState
{
Idle = 3,
Run,
Jump
}
If the enum starts at 3, you must ensure the array can accommodate indices [3], [4], and [5]. Otherwise, indices [0] to [2] are wasted memory.
Thus, it is best practice to define enum values sequentially starting from 0.
Using Dictionary with enum (Simple Example)
Instead of arrays, using a Dictionary allows easier management even if enum values are non-sequential. Here is a simple way to initialize a Dictionary:
var playerStateDic = new Dictionary<PlayerState, string>
{
{ PlayerState.Idle, "Currently in Idle state." },
{ PlayerState.Run, "Currently in Run state." },
{ PlayerState.Jump, "Currently in Jump state." }
};
This way, you can associate descriptions directly with each PlayerState value.
Example of using the Dictionary
string runStateText = playerStateDic[PlayerState.Run];
Debug.Log(runStateText);
// Output: Currently in Run state.
Automatically populating a Dictionary by iterating through an enum
If you want to automatically populate the Dictionary based on enum values, you can use a simple foreach loop:
var playerStateDic = new Dictionary<PlayerState, string>();
foreach (PlayerState state in Enum.GetValues(typeof(PlayerState)))
{
playerStateDic.Add(state, "Currently in " + state + " state.");
}
Verification Example
string idleText = playerStateDic[PlayerState.Idle];
Debug.Log(idleText);
// Output: Currently in Idle state.
string jumpText = playerStateDic[PlayerState.Jump];
Debug.Log(jumpText);
// Output: Currently in Jump state.
Conclusion
Enums improve code readability. Using a Dictionary with enums allows much cleaner and safer management of states. Especially when associating descriptions or names with different states, enums combined with Dictionary are very effective. Please take this opportunity to practice using enums and Dictionary together!
