C#idiomatic C#
ca2217fundamentals/code-analysis/quality-rules

Code analysis

CA2217: Do not mark enums with FlagsAttribute

Learn about code analysis rule CA2217: Do not mark enums with FlagsAttribute

CA2217 Usage enabled by default in .NET 10: No
Fix is breaking or non-breaking Non-breakingApplicable languages C# and Visual Basic

Cause

An enumeration is marked with System.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.

By default, this rule only looks at externally visible enumerations, but this is configurable.

Rule description

An enumeration should have System.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 System.FlagsAttribute from the enumeration.

When to suppress warnings

Do not suppress a warning from this rule.

Configure code to analyze

Use the following option to configure which parts of your codebase to run this rule on.

You can configure this option for just this rule, for all rules it applies to, or for all rules in this category (Usage) that it applies to. For more information, see Code quality rule configuration options.

Include specific API surfaces

You can configure which parts of your codebase to run this rule on, based on their accessibility, by setting the api_surface option. For example, to specify that the rule should run only against the non-public API surface, add the following key-value pair to an .editorconfig file in your project:

ini
dotnet_code_quality.CAXXXX.api_surface = private, internal

Examples

The following code shows an enumeration, Color, that contains the value 3. 3 is not a power of two, or a combination of any of the defined values. The Color enumeration shouldn't be marked with System.FlagsAttribute.

sample · csharpdocs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca2217.cssnippet1

csharp
using System;

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

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

sample · vbdocs/fundamentals/code-analysis/quality-rules/snippets/vb/all-rules/ca2217-do-not-mark-enums-with-flagsattribute_1.vb

vb
Imports System

Namespace Samples

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

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

    End Enum
End Namespace

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

sample · csharpdocs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca2217.cssnippet2

csharp
using System;

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

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

sample · vbdocs/fundamentals/code-analysis/quality-rules/snippets/vb/all-rules/ca2217-do-not-mark-enums-with-flagsattribute_2.vb

vb
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