This documentation is archived and is not being maintained.
FlagsAttribute Constructor
Visual Studio 2010
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 namespace System; // Define an Enum without FlagsAttribute. enum class SingleHue : short { Black = 0, Red = 1, Green = 2, Blue = 4 }; // Define an Enum with FlagsAttribute. [FlagsAttribute] enum class MultiHue : short { Black = 0, Red = 1, Green = 2, Blue = 4 }; int main() { Console::WriteLine( "This example of the FlagsAttribute attribute \n" "generates the following output." ); Console::WriteLine( "\nAll possible combinations of values of an \n" "Enum without FlagsAttribute:\n" ); // Display all possible combinations of values. for ( int val = 0; val <= 8; val++ ) Console::WriteLine( "{0,3} - {1}", val, ((SingleHue)val).ToString() ); Console::WriteLine( "\nAll possible combinations of values of an \n" "Enum with FlagsAttribute:\n" ); // Display all possible combinations of values. // Also display an invalid value. for ( int val = 0; val <= 8; val++ ) Console::WriteLine( "{0,3} - {1}", val, ((MultiHue)val).ToString() ); } /* 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 */
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Show: