C#中的[Flags]枚举属性是什么意思? [英] What does the [Flags] Enum Attribute mean in C#?

查看:367
本文介绍了C#中的[Flags]枚举属性是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候我会看到一个枚举如下:

  [Flags] 
public enum Options
{
无= 0,
Option1 = 1,
Option2 = 2,
Option3 = 4,
Option4 = 8
}

我不明白什么是 [Flags]

解决方案




任何人都可以发布一个很好的解释或例子, / div>

每当枚举代表标志的集合而不是单个值时,都应该使用flags属性。通常使用按位运算符操作这样的集合,例如:

  myProperties.AllowedColors = MyColor.Red | MyColor.Green | MyColor.Blue; 

请注意, [Flags] 本身不不要更改 - 它所做的一切都是通过 .ToString()方法:

 枚举西装{Spades = 1,Clubs = 2,Diamonds = 4,Hearts = 8} 
[Flags]枚举SuitsFlags {Spades = Clubs = 2,Diamonds = 4,Hearts = 8}

...

var str1 =(Suits.Spades | Suits.Diamonds).ToString();
//5
var str2 =(SuitsFlags.Spades | SuitsFlags.Diamonds).ToString();
//黑桃,钻石

同样重要的是注意 [Flags] 自动将枚举值设为两个。如果省略数值,枚举将无法正常运行,因为按位运算可能会出现这种情况,因为默认情况下,值从0开始递增。



声明不正确:

  [Flags] 
public枚举MyColors
{
黄色,
绿色,
红色,
蓝色
}

如果以这种方式声明,这些值将为黄色= 0,绿色= 1,红色= 2,蓝色= 3。这将使其无用作标志。



以下是正确声明的示例:

  [Flags] 
public enum MyColors
{
黄色= 1,
绿色= 2,
红色= 4,
蓝色= 8
} $ b要获取属性中的不同值,可以执行以下操作:


$ p

$ b

  if((myProperties.AllowedColors& MyColor.Yellow)== MyColor.Yellow)
{
//黄色已设置..
}

if((myProperties.AllowedColors& MyColor.Green)== MyColor.Green)
{
//绿色已设置...
}

或在.NET 4及更高版本中: / p>

  if(myProperties.AllowedColors.HasFlag(MyColor.Yellow))
{
//黄已经设置...
}

覆盖下 p>

这是因为您以前在枚举中使用了两个权力。在封面下,你的枚举值看起来像这样(以字节为单位,有8位可以是1或0)

 黄色:00000001 
绿色:00000010
红色:00000100
蓝色:00001000

同样,在将属性设置为允许的颜色设置为红色,绿色和蓝色(其中管道的OR值为哪个值)时,允许的颜色看起来像这样

  myProperties.AllowedColors:00001110 

所以当你检索这个值的时候,你实际上是按照和的值

  myProperties.AllowedColors :00001110 
MyColor.Green:00000010
-----------------------
00000010 //嘿,这是和MyColor.Green一样!

无= 0值



关于在你枚举中使用0,引用msdn:

  [Flags] 
public枚举MyColors
{
无= 0,
....
}




使用None作为值为零的枚举常量的标志名称。 您不能使用按位AND操作中的无枚举常数来测试标志,因为结果始终为零。但是,您可以执行逻辑,而不是按位来进行数值和无枚举常数来确定数值中是否有任何位被设置。


您可以在 msdn 在msdn设计标志


From time to time I see an enum like the following:

[Flags]
public enum Options 
{
    None    = 0,
    Option1 = 1,
    Option2 = 2,
    Option3 = 4,
    Option4 = 8
}

I don't understand what exactly the [Flags]-attribute does.

Anyone have a good explanation or example they could post?

解决方案

The flags attribute should be used whenever the enumerable represents a collection of flags, rather than a single value. Such collections are usually manipulated using bitwise operators, for example:

myProperties.AllowedColors = MyColor.Red | MyColor.Green | MyColor.Blue;

Note that [Flags] by itself doesn't change this at all - all it does is enable a nice representation by the .ToString() method:

enum Suits { Spades = 1, Clubs = 2, Diamonds = 4, Hearts = 8 }
[Flags] enum SuitsFlags { Spades = 1, Clubs = 2, Diamonds = 4, Hearts = 8 }

...

var str1 = (Suits.Spades | Suits.Diamonds).ToString();
           // "5"
var str2 = (SuitsFlags.Spades | SuitsFlags.Diamonds).ToString();
           // "Spades, Diamonds"

It is also important to note that [Flags] does not automatically make the enum values powers of two. If you omit the numeric values, the enum will not work as one might expect in bitwise operations, because by default the values start with 0 and increment.

Incorrect declaration:

[Flags]
public enum MyColors
{
    Yellow,
    Green,
    Red,
    Blue
}

The values, if declared this way, will be Yellow = 0, Green = 1, Red = 2, Blue = 3. This will render it useless for use as flags.

Here's an example of a correct declaration:

[Flags]
public enum MyColors
{
    Yellow = 1,
    Green = 2,
    Red = 4,
    Blue = 8
}

To retrieve the distinct values in your property, one can do this:

if((myProperties.AllowedColors & MyColor.Yellow) == MyColor.Yellow)
{
    // Yellow has been set...
}

if((myProperties.AllowedColors & MyColor.Green) == MyColor.Green)
{
    // Green has been set...
}    

or, in .NET 4 and later:

if (myProperties.AllowedColors.HasFlag(MyColor.Yellow))
{
    // Yellow has been set...
}

Under the covers

This works because you previously used powers of two in your enumeration. Under the covers, your enumeration values look like this (presented as bytes, which has 8 bits which can be 1's or 0's)

 Yellow: 00000001
 Green:  00000010
 Red:    00000100
 Blue:   00001000

Likewise, after you've set your property AllowedColors to Red, Green and Blue (which values where OR'ed by the pipe |), AllowedColors looks like this

myProperties.AllowedColors: 00001110

So when you retrieve the value you are actually bitwise AND'ing the values

myProperties.AllowedColors: 00001110
             MyColor.Green: 00000010
             -----------------------
                            00000010 // Hey, this is the same as MyColor.Green!

The None = 0 value

And regarding use 0 in you enumeration, quoting from msdn:

[Flags]
public enum MyColors
{
    None = 0,
    ....
}

Use None as the name of the flag enumerated constant whose value is zero. You cannot use the None enumerated constant in a bitwise AND operation to test for a flag because the result is always zero. However, you can perform a logical, not a bitwise, comparison between the numeric value and the None enumerated constant to determine whether any bits in the numeric value are set.

You can find more info about the flags attribute and its usage at msdn and designing flags at msdn

这篇关于C#中的[Flags]枚举属性是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆