由于我的游戏有一些模式(应该在初始化时提供),所以我想为它创建一个枚举.后来我想得到这个枚举的价值.以下是我的代码 -
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我很想知道一个更好的方法来做同样的事情,如果存在的话.
Hei*_*nzi 11
当然,有一种更简单的方法.只需将枚举转换为其基础数值数据类型:
value = (short)mode;
Run Code Online (Sandbox Code Playgroud)