FlagsAttribute クラス

列挙体をビット フィールド、つまりフラグのセットとして扱えることを示します。

この型のすべてのメンバの一覧については、FlagsAttribute メンバ を参照してください。

System.Object
   System.Attribute
      System.FlagsAttribute

<AttributeUsage(AttributeTargets.Enum)>
<Serializable>
Public Class FlagsAttribute   Inherits Attribute
[C#]
[AttributeUsage(AttributeTargets.Enum)]
[Serializable]
public class FlagsAttribute : Attribute
[C++]
[AttributeUsage(AttributeTargets::Enum)]
[Serializable]
public __gc class FlagsAttribute : public Attribute
[JScript]
public
   AttributeUsage(AttributeTargets.Enum)
 Serializable
class FlagsAttribute extends Attribute

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

ビットごとの OR 演算を使用してビット フィールドを組み合わせることができますが、列挙定数を組み合わせることはできません。

通常、ビット フィールドは、組み合わせて使用できる要素のリストとして使用されます。一方、列挙定数は、同時には選択できない要素のリストとして使用されます。つまり、ビット フィールドは、ビットごとの OR 演算で組み合わせて無名の値を作成できるような設計になっていますが、列挙定数はそのようには設計されていません。言語によって、列挙定数とビット フィールドの使用方法の違いはさまざまです。

AttributeUsageAttribute はこのクラスに適用されます。また、その Inherited プロパティは false を指定します。この属性を適用できるのは列挙体だけです。

使用例

[Visual Basic, C#, C++] FlagsAttribute 属性を Enum 宣言で使用した場合の ToString メソッドへの影響を示すコード例を次に示します。

 
' Example of the FlagsAttribute attribute.
Imports System
Imports Microsoft.VisualBasic

Module FlagsAttributeDemo
    
    ' 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

    Sub Main( )
        Console.WriteLine( _
            "This example of the FlagsAttribute attribute " & _
            vbCrLf & "generates the following output." )
        Console.WriteLine( vbCrLf & _
            "All possible combinations of values of an " & _
            vbCrLf & "Enum without FlagsAttribute:" & vbCrLf )
        
        ' Display all possible combinations of values.
        Dim val as Integer
        For val = 0 to 8
            Console.WriteLine( "{0,3} - {1}", _
                val, CType( val, SingleHue ).ToString( ) )
        Next val

        Console.WriteLine( vbCrLf & _
            "All possible combinations of values of an " & _
            vbCrLf & "Enum with FlagsAttribute:" & vbCrLf )
        
        ' Display all possible combinations of values.
        ' Also display an invalid value.
        For val = 0 to 8
            Console.WriteLine( "{0,3} - {1}", _
                val, CType( val, MultiHue ).ToString( ) )
        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

[C#] 
// Example of the FlagsAttribute attribute.
using System;

class FlagsAttributeDemo
{
    // 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
    };

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

[C++] 
// Example of the FlagsAttribute attribute.
#using <mscorlib.dll>
using namespace System;

// Define an Enum without FlagsAttribute.
__value enum SingleHue : short
{
    Black = 0,
    Red = 1,
    Green = 2,
    Blue = 4
};

// Define an Enum with FlagsAttribute.
[FlagsAttribute] 
__value enum MultiHue : short
{
    Black = 0,
    Red = 1,
    Green = 2,
    Blue = 4
};

void main( )
{
    Console::WriteLine( 
        S"This example of the FlagsAttribute attribute \n" 
        S"generates the following output." );
    Console::WriteLine( 
        S"\nAll possible combinations of values of an \n" 
        S"Enum without FlagsAttribute:\n" );
    
    // Display all possible combinations of values.
    for( int val = 0; val <= 8; val++ )
        Console::WriteLine( S"{0,3} - {1}", 
            __box( val ), __box( (SingleHue)val )->ToString( ) );

    Console::WriteLine( 
        S"\nAll possible combinations of values of an \n" 
        S"Enum with FlagsAttribute:\n" );
    
    // Display all possible combinations of values.
    // Also display an invalid value.
    for( int val = 0; val <= 8; val++ )
        Console::WriteLine( S"{0,3} - {1}", 
            __box( val ), __box( (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
*/

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

アセンブリ: Mscorlib (Mscorlib.dll 内)

参照

FlagsAttribute メンバ | System 名前空間