本帖最后由 DWBJ 于 2014-2-16 11:53 编辑
C#枚举的定义与使用
1.枚举的定义:enum(枚举)和类是一个级别的,定义枚举要在类之外定义,定义枚举时可赋初始值,也可不赋。
2.枚举的使用和意义:使用时在Main函数内执行,定义枚举意义是限制值的取值范围,一句话归类。
namespace ConsoleApplication3
{
enum weekday
{
Mon=1,Tue=2,Wed=3,Thu=4,Fri=5,Sat=6,Sun=7
}
class Program
static void Main(string[] args)
{
weekday wd1 = weekday.Mon;
weekday wd2 = weekday.Tue;
weekday wd3 = weekday.Wed;
weekday wd4 = weekday.Thu;
weekday wd5 = weekday.Fri;
weekday wd6 = weekday.Sat;
weekday wd7 = weekday.Sun;
Console.WriteLine(wd1);
Console.WriteLine(wd2);
Console.WriteLine(wd3);
Console.WriteLine(wd4);
Console.WriteLine(wd5);
Console.WriteLine(wd6);
Console.WriteLine(wd7);
}
} |