using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { //声明枚举类型 enum orientation : byte { north = 1, south = 2, east = 3, west = 4 } class Program { static void Main(string[] args) { //枚举使用一个基本类型来存储,枚举类型可以提取的每个值都存储为该基本类型的一个值,默认情况下该类型为int; //枚举的基本类型可以是byte,sbyte,short,ushort,int,uint,long,ulong; //调用枚举类型 orientation myDirection = orientation.north; Console.WriteLine("myDirection={0}",myDirection); //将string类型转换为枚举类型,typeof(orientation)将得到orientation的类型,Parse()将进行类型转换; string myString = "north"; orientation myDirection2 = (orientation)Enum.Parse(typeof(orientation), myString); Console.WriteLine("myDirection2={0}", myDirection2); Console.ReadLine(); } } }
转载于:https://blog.51cto.com/57388/1650554