ToString()方法将此实例的值转换为其等效的字符串表示形式。
首先,设置一个枚举。
enum Vehicle { Car, Bus, Truck, Motobike };
要将其转换为等效的字符串表示形式,请使用ToString()。
Vehicle.Car.ToString("d")
using System; public class Demo { enum Vehicle { Car, Bus, Truck, Motobike }; public static void Main() { Console.WriteLine("Vehicle.Car = {0}", Vehicle.Car.ToString("d")); Console.WriteLine("Vehicle.Bus = {0}", Vehicle.Bus.ToString("d")); } }
输出结果
Vehicle.Car = 0 Vehicle.Bus = 1