次の方法で共有


CA2217: Do not mark enums with FlagsAttribute

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Item Value
TypeName DoNotMarkEnumsWithFlags
CheckId CA2217
Category Microsoft.Usage
Breaking Change Non Breaking

Cause

An externally visible enumeration is marked with FlagsAttribute and it has one or more values that are not powers of two or a combination of the other defined values on the enumeration.

Rule Description

An enumeration should have FlagsAttribute present only if each value defined in the enumeration is a power of two, or a combination of defined values.

How to Fix Violations

To fix a violation of this rule, remove FlagsAttribute from the enumeration.

When to Suppress Warnings

Do not suppress a warning from this rule.

Example

The following example shows an enumeration, Color, that contains the value 3, which is neither a power of two, nor a combination of any of the defined values. The Color enumeration should not be marked with the FlagsAttribute.

using namespace System;
 
namespace Samples 
{
    // Violates this rule    
    [FlagsAttribute]    
    public enum class Color    
    {        
        None   = 0,        
        Red    = 1,        
        Orange = 3,        
        Yellow = 4    
    };
}
using System;

namespace Samples
{    
    // Violates this rule    
    [FlagsAttribute]        
    public enum Color
    { 
        None    = 0, 
        Red     = 1, 
        Orange  = 3, 
        Yellow  = 4 
    }
}

Imports System

Namespace Samples

    ' Violates this rule    
    <FlagsAttribute()> _
    Public Enum Color

        None = 0
        Red = 1
        Orange = 3
        Yellow = 4

    End Enum
End Namespace

Example

The following example shows an enumeration, Days, that meets the requirements for being marked with the System.FlagsAttribute.

using namespace System;
 
namespace Samples 
{    
    [FlagsAttribute]    
    public enum class Days    
    {        
        None        = 0,        
        Monday      = 1,        
        Tuesday     = 2,        
        Wednesday   = 4,        
        Thursday    = 8,        
        Friday      = 16,        
        All         = Monday| Tuesday | Wednesday | Thursday | Friday    
    };
}
using System;

namespace Samples
{    
    [FlagsAttribute]    
    public enum Days    
    {        
        None        = 0,        
        Monday      = 1,        
        Tuesday     = 2,        
        Wednesday   = 4,        
        Thursday    = 8,        
        Friday      = 16,        
        All         = Monday| Tuesday | Wednesday | Thursday | Friday    
    }
}

Imports System
Namespace Samples

    <FlagsAttribute()> _
    Public Enum Days

        None = 0
        Monday = 1
        Tuesday = 2
        Wednesday = 4
        Thursday = 8
        Friday = 16
        All = Monday Or Tuesday Or Wednesday Or Thursday Or Friday

    End Enum
End Namespace

CA1027: Mark enums with FlagsAttribute

See Also

System.FlagsAttribute