在C#中具有自定义值的枚举
Enum是枚举,用于存储一组命名常量,例如年,产品,月,季等。
Enum常数的默认值从0开始递增。它具有一组固定的常数,可以轻松地遍历。但是,您仍然可以更改开始索引并使用您选择的值对其进行自定义。
在以下示例中,我将自定义值设置为20,而不是默认值0。
示例
using System; public class Demo { public enum Vehicle { Car =20, Motorcycle, Bus, Truck } public static void Main() { int a = (int)Vehicle.Car; int b = (int)Vehicle.Motorcycle; int c = (int)Vehicle.Bus; int d = (int)Vehicle.Truck; Console.WriteLine("Car = {0}", a); Console.WriteLine("Motorcycle = {0}", b); Console.WriteLine("Bus = {0}", c); Console.WriteLine("Truck = {0}", d); } }
输出结果
Car = 20 Motorcycle = 21 Bus = 22 Truck = 23