使用[Flags]属性标记枚举的原因是什么?

B. *_*non 5 c# resharper enums bit-manipulation

我们最近将一个旧的C#应用​​程序从Visual Studio 2003移植到Visual Studio 2010.

在搜索代码中要清理的东西时,我在上面运行了Resharper,它告诉我(很多次),"枚举上的按位操作没有标记[Flags]属性"

例如,这里有一些代码,它用该消息"标记"(没有双关语):

~DuckbillConverter()
{
    //stop running thread
    if((this.Status & ConvertStatus.Running) == ConvertStatus.Running)
        this.Stop();
}
Run Code Online (Sandbox Code Playgroud)

我假设这段代码按原样运行; 那么,如果R#建议用[Flags]属性装饰ConvertStatus.Running会有什么好处(或者更重要的是,任何可能的副作用)?

UPDATE

回答Jon Skeet:

public enum ConvertStatus
{
    /// <summary>
    /// The thread is not running, there are no manual conversions or purges and no errors have occurred.
    /// </summary>
    Stopped = 0x0,
    /// <summary>
    /// The thread is running and will automatically convert all sites for both file types.
    /// </summary>
    Running = 0x1,
    /// <summary>
    /// A data conversion is currently taking place.
    /// </summary>
    Converting = 0x2,
    /// <summary>
    /// A data purge is currently taking place.
    /// </summary>
    Purging = 0x4,
    /// <summary>
    /// An error has occurred.  Use the LastError property to view the error message.
    /// </summary>
    Error = 0x8
}
Run Code Online (Sandbox Code Playgroud)

Pre*_*lot 8

Resharper不建议ConvertStatus.Running接收属性,而是整个ConvertStatus枚举.

来自MSDN,FlagsAttribute被描述为:

表示可以将枚举视为位字段; 也就是说,一组标志.

由于您对指示实际可用作位字段的枚举使用按位运算,因此R#警告您代码可能在运行时具有意外影响.使用[Flags]属性本身并不能保证枚举的实现实际上与位字段的预期值一致,但它确实创建了外部世界应该期望的合同.