static void Main2() { //0000000001 | 0000000010 =》0000000011 PrintPersonStyle(PersonStye.tall | PersonStye.rich); //数据类型转换 //int ==>Enum PersonStye style01 = (PersonStye)2; PrintPersonStyle(style01); //PrintPersonStyle((PersonStye)2); //Enum ==>int int enumNumber = (int)(PersonStye.beauty | PersonStye.handsome); //string ==>Enum //"beauty" PersonStye style02 = (PersonStye)Enum.Parse(typeof(PersonStye), "beauty"); //Enum ==>string string strEnum = PersonStye.handsome.ToString(); }
//枚举 类和对象 private static void PrintPersonStyle(PersonStye style) { //包含 if ((style & PersonStye.tall) == PersonStye.tall) { Console.WriteLine("大个子"); } if ((style & PersonStye.rich) == PersonStye.rich) { Console.WriteLine("富豪"); } if ((style & PersonStye.handsome) != 0) { Console.WriteLine("英俊"); } if ((style & PersonStye.white) == PersonStye.white) { Console.WriteLine("洁白"); } if ((style & PersonStye.beauty) != 0) { Console.WriteLine("漂亮"); } }