The source code to demonstrate the Flags attribute is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
//Program to demonstrate the FlagAttribute in C#
using System;
class Sample
{
enum Weeks
{
Sun = 1, Mon = 2, Tue = 4, Wed = 8,
}
[Flags]enum WeekFlags
{
Sun = 1, Mon = 2, Tue = 4, Wed = 8
}
// Main Method
public static void Main(string[] args)
{
Console.WriteLine((Weeks.Tue | Weeks.Wed).ToString());
Console.WriteLine((WeekFlags.Tue | WeekFlags.Wed).ToString());
}
}
Output:
12
Tue, Wed
Press any key to continue . . .
Explanation:
In the above program, we created a Sample class that contains two enumerations Weeks and WeekFlags. Here, WeekFlags enumeration is declared with Flags attribute. The Sample class also contains the Main() method. The Main() method is the entry point for the program.
The above statement will print "Tue, Wed" after performing bitwise or operation on the console screen because we used Flags attribute with WeekFlags enumeration.
Program:
The source code to demonstrate the Flags attribute is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
Output:
Explanation:
In the above program, we created a Sample class that contains two enumerations Weeks and WeekFlags. Here, WeekFlags enumeration is declared with Flags attribute. The Sample class also contains the Main() method. The Main() method is the entry point for the program.
The above statement will print 12 after performing bitwise or operation on the console screen.
The above statement will print "Tue, Wed" after performing bitwise or operation on the console screen because we used Flags attribute with WeekFlags enumeration.