Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Expand Minimize

CA2217: Do not mark enums with FlagsAttribute







Breaking Change

Non Breaking

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.

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.

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

Do not suppress a warning from this rule.

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 System;

namespace Samples
    // Violates this rule    
    public enum Color
        None    = 0, 
        Red     = 1, 
        Orange  = 3, 
        Yellow  = 4 

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

using System;

namespace Samples
    public enum Days    
        None        = 0,        
        Monday      = 1,        
        Tuesday     = 2,        
        Wednesday   = 4,        
        Thursday    = 8,        
        Friday      = 16,        
        All         = Monday| Tuesday | Wednesday | Thursday | Friday    
© 2015 Microsoft