Skip to content

Gendarme.Rules.Design.UseFlagsAttributeRule(git)

Sebastien Pouliot edited this page Mar 2, 2011 · 1 revision

UseFlagsAttributeRule

Assembly: Gendarme.Rules.Design
Version: git

Description

This rule will fire if an enum's values look like they are intended to be composed together with the bitwise OR operator and the enum is not decorated with System.FlagsAttribute. Using FlagsAttribute will allow System.Enum.ToString() to return a better string when values are ORed together and helps indicate to readers of the code the intended usage of the enum.

Examples

Bad example:

[Serializable]
enum Options {
    First = 1,
    Second = 2,
    Third = 4,
    All = First | Second | Third,
}

Good example:

[Flags]
[Serializable]
enum Options {
    First = 1,
    Second = 2,
    Third = 4,
    All = First | Second | Third,
}

Notes

  • This rule is available since Gendarme 2.6

Source code

You can browse the latest source code of this rule on github.com

Clone this wiki locally