This question already has answers here:
Get int value from enum in C#
                                
                                    (27个答案)
                                
                        
                                5年前关闭。
            
                    
我有以下枚举:

 public enum BikeType : byte
  {
    Road = 0,
    Mountain = 1
  };


当我尝试将其传递给a时,我检索了字节的“字符串”表示形式,而不是数字值:

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

"Road bike has a byte value of Road"


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

谢谢

最佳答案

您需要将其转换为int

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


如果不进行转换,它将在ToString上调用BikeType.Road,它将返回Road

关于c# - 具有基础类型的枚举。意外返回字符串表示形式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21020014/

10-09 03:20