FlagsAttribute Constructor
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Initializes a new instance of the FlagsAttribute class.
Assembly: mscorlib (in mscorlib.dll)
The following code example illustrates the use of the FlagsAttribute attribute and shows the effect on the ToString method of using FlagsAttribute on an Enum declaration.
// Example of the FlagsAttribute attribute. using System; class Example { // Define an Enum without FlagsAttribute. enum SingleHue : short { Black = 0, Red = 1, Green = 2, Blue = 4 }; // Define an Enum with FlagsAttribute. [FlagsAttribute] enum MultiHue : short { Black = 0, Red = 1, Green = 2, Blue = 4 }; public static void Demo(System.Windows.Controls.TextBlock outputBlock) { outputBlock.Text += "This example of the FlagsAttribute attribute \n" + "generates the following output." + "\n"; outputBlock.Text += "\nAll possible combinations of values of an \n" + "Enum without FlagsAttribute:\n" + "\n"; // Display all possible combinations of values. for (int val = 0; val <= 8; val++) outputBlock.Text += String.Format("{0,3} - {1}", val, ((SingleHue)val).ToString()) + "\n"; outputBlock.Text += String.Format( "\nAll possible combinations of values of an \n" + "Enum with FlagsAttribute:\n") + "\n"; // Display all possible combinations of values. // Also display an invalid value. for (int val = 0; val <= 8; val++) outputBlock.Text += String.Format("{0,3} - {1}", val, ((MultiHue)val).ToString()) + "\n"; } } /* This example of the FlagsAttribute attribute generates the following output. All possible combinations of values of an Enum without FlagsAttribute: 0 - Black 1 - Red 2 - Green 3 - 3 4 - Blue 5 - 5 6 - 6 7 - 7 8 - 8 All possible combinations of values of an Enum with FlagsAttribute: 0 - Black 1 - Red 2 - Green 3 - Red, Green 4 - Blue 5 - Red, Blue 6 - Green, Blue 7 - Red, Green, Blue 8 - 8 */
Show: