Export (0) Print
Expand All

FlagsAttribute Constructor

Initializes a new instance of the FlagsAttribute class.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public FlagsAttribute()

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
*/


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2014 Microsoft