以下是枚举权值位运行示例:
public static void RunSnippet()
{
//对枚举值进行按位或运算
ClassStatisticsType d = ClassStatisticsType.ApplyAudition | ClassStatisticsType.Approved;
ClassStatisticsType k = ClassStatisticsType.Audition | ClassStatisticsType.GiftClass|ClassStatisticsType.Normal;
if ((d&ClassStatisticsType.Approved)!=0)
WL("枚举值为:"+ClassStatisticsType.Approved);
else
WL("未找到Approved枚举值!");
if ((k&ClassStatisticsType.Required)!=0)
WL("枚举值为:"+ClassStatisticsType.Required);
else
WL("未找到Required枚举值!");
WL("移位后的值:"+(1 << 5));
}
/// <summary>
/// 枚举
/// 说明:
/// 1、[Flags]标识枚举可以用于权值位运算
/// 2、<< 用于对左移运算,将左边数移动第二个数到指定的位数,1 << 0:将左边数字1移动0,则返回的值保持不变。移动的位数为2的N次方
/// </summary>
[Flags]
public enum ClassStatisticsType
{
/// <summary>
/// 申请试听
/// </summary>
ApplyAudition = 1 << 0,
/// <summary>
/// 应到
/// </summary>
Required = 1 << 1,
/// <summary>
/// 到课
/// </summary>
Participation = 1 << 2,
/// <summary>
/// 普通上课
/// </summary>
Normal = 1 << 3,
/// <summary>
/// 试听上课
/// </summary>
Audition = 1 << 4,
/// <summary>
/// 赠课上课
/// </summary>
GiftClass = 1 << 5,
/// <summary>
/// 预约
/// </summary>
Reservation = 1 << 6,
/// <summary>
/// 已批
/// </summary>
Approved = 1 << 7,
/// <summary>
/// 上限
/// </summary>
UpperLimit = 1 << 8,
/// <summary>
/// 未批
/// </summary>
UnApproved = 1 << 9
}
#region Helper methods
public static void Main()
{
try
{
RunSnippet();
}
catch (Exception e)
{
string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());
Console.WriteLine(error);
}
finally
{
Console.Write("Press any key to continue...");
Console.ReadKey();
}
}
private static void WL(object text, params object[] args)
{
Console.WriteLine(text.ToString(), args);
}