具有基础类型的枚举.无意中返回字符串表示

nf3*_*743 1 c# enums

我有以下枚举:

 public enum BikeType : byte
  {
    Road = 0,
    Mountain = 1
  };
Run Code Online (Sandbox Code Playgroud)

当我尝试将它传递给我时,我检索字节的'字符串'表示,而不是数字值:

string str = string.Format("Road bike has a byte value of {0}", BikeType.Road);

"Road bike has a byte value of Road"
Run Code Online (Sandbox Code Playgroud)

我想要字节值(0).我究竟做错了什么?

谢谢

Kam*_*ski 6

你需要做int到int

string str = 
          string.Format("Road bike has a byte value of {0}", (int)BikeType.Road);
Run Code Online (Sandbox Code Playgroud)

如果你不投它,它会调用ToStringBikeType.Road,这将返回Road