Flag enum

We can use Flag enum value to do bitwise operation.


using System;

[Flags]
public enum WeekDay{

   Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday

}

class Test
{
    static void Main()
    {
        WeekDay day = WeekDay.Saturday | WeekDay.Sunday;
        Console.WriteLine(day);
    }
}

The output:


Tuesday, Sunday

Check the combination

 
using System;

[Flags]
public enum WeekDay
{

    Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday

}

class Test
{
    static void Main()
    {
        WeekDay day = WeekDay.Saturday | WeekDay.Sunday;
        if ((day & WeekDay.Saturday) != 0)
        {
            Console.WriteLine("it is a weekend.");
        }

    }
}

The output:


it is a weekend.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.