using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EnumTest
{
class Program
{
//枚举类型的定义,通常默认为int类型,可以通过第二种方式指定另一种整数值类型。
//枚举支持的类型有byte,sbyte,short,ushort,int,uint,long或ulong
//枚举类型,默认从0开始,增量为1,依次递增
private enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
private enum Months : byte { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec };
//枚举类型也可以通过给每个
/*使用枚举的好处
* 1.只看个数字,不能看出值代表的含义
* 2.vs编程时,IntelliSense列出定义的值
*/
//enum还可以与Flags特性配合使用
[Flags]
enum Days2
{
None = 0x0,
Sunday = 0x1,
Monday = 0x2,
Tuesday = 0x4,
Wednesday =0x8,
Thurday = 0x10,
Friday = 0x20,
Saturday = 0x40
}
static void Main(string[] args)
{
Days today = Days.Monday;
int dayNumber = (int)today;
Console.WriteLine("{0} is day number #{1}", today, dayNumber);
Months thisMonth = Months.Mar;
byte monthNumber = (byte)thisMonth;
Console.WriteLine("{0} is month number #{1}", thisMonth, monthNumber);
//表示星期二和星期四开会
Days2 meetingDays = Days2.Tuesday | Days2.Thurday;
//如果要加一天星期五
meetingDays = meetingDays | Days2.Friday;
Console.WriteLine("Meeting days are {0}",meetingDays);
//减去一天
meetingDays = meetingDays ^ Days2.Tuesday;
Console.WriteLine("Meeting days are {0}", meetingDays);
Console.Read();
}
}
}
登山-C#-enum类型基础
最新推荐文章于 2023-10-06 22:02:22 发布