Working with enumeration in C #

Since there are several modes in my game (which should be provided during initialization), I thought about creating an enumeration for it. Later I wanted to get the meaning of this listing. Below is my code -

enum GameMode : short
{
    Stop = 0,
    StartSinglePlayer = 4,
    StartMultiPlayer = 10,
    Debug = 12
}
class Game
{
    static short GetValue(GameMode mode)
    {
        short index = -1;
        if (mode == GameMode.Stop)
            index = 0;
        else if (mode == GameMode.StartSinglePlayer)
            index = 4;
        else if (mode == GameMode.StartMultiPlayer)
            index = 10;
        else if (mode == GameMode.Debug)
            index = 12;
        return index;
    }
    static void Main(string[] args)
    {
        var value = GetValue(GameMode.StartMultiPlayer);
    }
}

I am curious about the best way to do the same, if it exists.

+4
source share
2 answers

Of course, there is a much simpler way. Just translate your enum to its base numeric data type:

value = (short)mode;
+11
source

, , - , GameMode, Heinzi . , , , , GameMode, .

+1

Source: https://habr.com/ru/post/1534061/


All Articles