using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace unit5
{
class Program
{
//枚举
enum Week : short //指定枚举类型中的值为short类型(默认情况下是 int 类型)
{
Monday = 1,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
static void Main(string[] args)
{
//枚举的使用
byte byteDay;
byte myByte;
string myString;
Week day = Week.Friday;
byteDay = checked((byte)day); //将枚举类型转换为byte类型
Console.WriteLine(byteDay);
//byte转换为枚举类型
myByte = 3; //值为3,映射过去显示Wednesday
day = (Week)myByte;
Console.WriteLine(day);
myByte = 10; //值为10,枚举类型可以存储这个值,但是在枚举中没有对这个值的映射,
//所以在后面的操作中可能出现逻辑上的错误,这样做 没有意义
day = (Week)myByte;
Console.WriteLine(day);
//枚举转换为string
myString = day.ToString(); //注意:不可以用(string)day 这样的转换,有更复杂的转换,即用 Enum.Parse()
Console.WriteLine(day);
//Console.WriteLine(day); //显示的结果为Friday
//day++; //可以对枚举对象进行++操作
//Console.WriteLine(day);
Console.ReadKey();
}
}
}