C#枚举权值位运算示例

以下是枚举权值位运行示例:

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));
	}
	
	/// 
    /// 枚举
	/// 说明:
	/// 1、[Flags]标识枚举可以用于权值位运算
	/// 2、<< 用于对左移运算,将左边数移动第二个数到指定的位数,1 << 0:将左边数字1移动0,则返回的值保持不变。移动的位数为2的N次方
    /// 
    [Flags]
    public enum ClassStatisticsType
    {
        /// 
        /// 申请试听
        /// 
        ApplyAudition = 1 << 0,


        /// 
        /// 应到
        /// 
        Required = 1 << 1,


        /// 
        /// 到课
        /// 
        Participation = 1 << 2,


        /// 
        /// 普通上课
        /// 
        Normal = 1 << 3,


        /// 
        /// 试听上课
        /// 
        Audition = 1 << 4,


        /// 
        /// 赠课上课
        /// 
        GiftClass = 1 << 5,


        /// 
        /// 预约
        /// 
        Reservation = 1 << 6,


        /// 
        /// 已批
        /// 
        Approved = 1 << 7,


        /// 
        /// 上限
        /// 
        UpperLimit = 1 << 8,


        /// 
        /// 未批
        /// 
        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);	
	}


你可能感兴趣的:(C#,c#,位运算,枚举)