/// <summary>
/// 枚举
/// </summary>
enum TimeOfDay
{
Morning =1 ,
Afternoon =2,
Evening =3
}
/// <summary>
/// 使用枚举的类
/// </summary>
class Program
{
public static void Main(string[] args)
{
Greeting(TimeOfDay.Morning);
Greeting(TimeOfDay.Afternoon);
Greeting(TimeOfDay.Evening);
PrintValueOfDay(TimeOfDay.Morning);
GetNameByValue(1);
Console.ReadKey();
}
/// <summary>
/// 打招呼方法
/// </summary>
/// <param name="timeOfDay">每一天的时间段</param>
static void Greeting(TimeOfDay timeOfDay)
{
switch (timeOfDay)
{
case TimeOfDay.Morning:
Console.WriteLine("Hello Morning!");
break;
case TimeOfDay.Afternoon:
Console.WriteLine("Hello Afternoon!");
break;
case TimeOfDay.Evening:
Console.WriteLine("Hello Evening!");
break;
}
}
/// <summary>
/// 打印枚举变量的值
/// </summary>
/// <param name="timeOfDay">枚举变量</param>
private static void PrintValueOfDay(TimeOfDay timeOfDay)
{
//Enum.Parse()三个参数依次为枚举类型,要转化的字符串,是否忽略大小写
int result = Convert.ToInt32(Enum.Parse(typeof(TimeOfDay), timeOfDay.ToString(), true));
Console.WriteLine("枚举变量"+timeOfDay.ToString()+"的值为"+result);
}
/// <summary>
/// 由枚举名值获得名称
/// </summary>
/// <param name="value"></param>
private static void GetNameByValue(int value)
{
Console.WriteLine("枚举中"+value+"对应的枚举名是"+Enum.GetName(typeof(TimeOfDay),1));
}
/// 枚举
/// </summary>
enum TimeOfDay
{
Morning =1 ,
Afternoon =2,
Evening =3
}
/// <summary>
/// 使用枚举的类
/// </summary>
class Program
{
public static void Main(string[] args)
{
Greeting(TimeOfDay.Morning);
Greeting(TimeOfDay.Afternoon);
Greeting(TimeOfDay.Evening);
PrintValueOfDay(TimeOfDay.Morning);
GetNameByValue(1);
Console.ReadKey();
}
/// <summary>
/// 打招呼方法
/// </summary>
/// <param name="timeOfDay">每一天的时间段</param>
static void Greeting(TimeOfDay timeOfDay)
{
switch (timeOfDay)
{
case TimeOfDay.Morning:
Console.WriteLine("Hello Morning!");
break;
case TimeOfDay.Afternoon:
Console.WriteLine("Hello Afternoon!");
break;
case TimeOfDay.Evening:
Console.WriteLine("Hello Evening!");
break;
}
}
/// <summary>
/// 打印枚举变量的值
/// </summary>
/// <param name="timeOfDay">枚举变量</param>
private static void PrintValueOfDay(TimeOfDay timeOfDay)
{
//Enum.Parse()三个参数依次为枚举类型,要转化的字符串,是否忽略大小写
int result = Convert.ToInt32(Enum.Parse(typeof(TimeOfDay), timeOfDay.ToString(), true));
Console.WriteLine("枚举变量"+timeOfDay.ToString()+"的值为"+result);
}
/// <summary>
/// 由枚举名值获得名称
/// </summary>
/// <param name="value"></param>
private static void GetNameByValue(int value)
{
Console.WriteLine("枚举中"+value+"对应的枚举名是"+Enum.GetName(typeof(TimeOfDay),1));
}