首先,设置枚举-
public enum Vehicle { Car, Bus, Truck }
现在使用类型转换将枚举转换为int-
int a = (int)Vehicle.Car;
以下是将枚举转换为int的完整代码-
using System; public class Demo { public enum Vehicle { Car, Bus, Truck } public static void Main() { int a = (int)Vehicle.Car; int b = (int)Vehicle.Bus; int c = (int)Vehicle.Truck; Console.WriteLine("Car = {0}", a); Console.WriteLine("Bus = {0}", b); Console.WriteLine("Truck = {0}", c); } }
输出结果
Car = 0 Bus = 1 Truck = 2