枚举是一种复杂的数据类型,可以看做一个数据集合,下面来练练手吧:
一个星期七天是最好的枚举学习例子了。
首先判断是否周末:[C#] 纯文本查看 复制代码 namespace ConsoleApplication1
{
//在主函数外定义枚举
enum WeekDays {Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday}
class Program
{
static void Main(string[] args)
{
WeekDays today = WeekDays.Sunday;
if ((today == WeekDays.Saturday) || (today == WeekDays.Sunday))
Console.WriteLine("I love the weekends!");
else
Console.WriteLine("Back to work");
}
}
}
再看不是周末的例子:[C#] 纯文本查看 复制代码 namespace ConsoleApplication1
{
//在主函数外定义枚举
enum WeekDays {Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday}
class Program
{
static void Main(string[] args)
{
WeekDays today = WeekDays.Monday;
if ((today == WeekDays.Saturday) || (today == WeekDays.Sunday))
Console.WriteLine("I love the weekends!");
else
Console.WriteLine("Back to work");
}
}
}
|