Condividi tramite


FlagsAttribute Constructor

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Initializes a new instance of the FlagsAttribute class.

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

Syntax

'Declaration
Public Sub New
public FlagsAttribute()

Examples

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.

Module Example

   ' Define an Enum without FlagsAttribute.
   Enum SingleHue As Short
      Black = 0
      Red = 1
      Green = 2
      Blue = 4
   End Enum

   ' Define an Enum with FlagsAttribute.
   <FlagsAttribute()> _
   Enum MultiHue As Short
      Black = 0
      Red = 1
      Green = 2
      Blue = 4
   End Enum

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      outputBlock.Text &= _
          "This example of the FlagsAttribute attribute " & _
          vbCrLf & "generates the following output." & vbCrLf
      outputBlock.Text &= vbCrLf & _
          "All possible combinations of values of an " & _
          vbCrLf & "Enum without FlagsAttribute:" & vbCrLf & vbCrLf

      ' Display all possible combinations of values.
      Dim val As Integer
      For val = 0 To 8
         outputBlock.Text &= String.Format("{0,3} - {1}", _
             val, CType(val, SingleHue).ToString()) & vbCrLf
      Next val

      outputBlock.Text &= String.Format(vbCrLf & _
          "All possible combinations of values of an " & _
          vbCrLf & "Enum with FlagsAttribute:" & vbCrLf) & vbCrLf

      ' Display all possible combinations of values.
      ' Also display an invalid value.
      For val = 0 To 8
         outputBlock.Text &= String.Format("{0,3} - {1}", _
             val, CType(val, MultiHue).ToString()) & vbCrLf
      Next val
   End Sub
End Module

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

Version Information

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

Platforms

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