What does the [Flags] Enum Attribute mean in C#?
来源:互联网 发布:excel表格导入数据库 编辑:程序博客网 时间:2024/05/21 09:08
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 thisat all - all it does is enable a nice representation by the .ToString()
method:
[Flags] enum SuitsFlags { Spades = 1, Clubs = 2, Diamonds = 4, Hearts = 8 }enum Suits { 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 you 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 multiples of two in you enumeration. Under the covers your enumeration values looks 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 retreive 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 atmsdn and designing flags at msdn
转载地址:http://stackoverflow.com/questions/8447/what-does-the-flags-enum-attribute-mean-in-c
- What does the [Flags] Enum Attribute mean in C#?
- What does the distance attribute in DMatches mean?
- What does the first letter "c" mean in "calloc"?
- What does the \0 symbol mean in a C string?
- What does idl attribute mean in the W3C html5 standard document?
- What does (Aborted) mean in HttpWatch?
- In Javascript, what does this underscore mean?
- What does ^M character mean in Vim?
- What does “%.*s” mean in printf?
- What does Using filesort mean in MySQL?
- what does the three dots in “doInBackground(URL… urls)” mean?
- What does `return x ? : 1` mean in C language? [duplicate] stackoverflow
- What does it mean to write `static void * ptr = &ptr` in objective-c?
- what does boinc mean?
- what does "ERP" mean
- What does "V"mean
- What does "Vanilla" mean ?
- Discussion1: What does mandatory/required variable in a query mean?
- 我们应该奋斗!
- ProgressDialog进度对话框的用法
- 梁洛施称爱情没年龄界限 李泽楷不是唯一选择
- 内置锁
- Python模块学习之hashlib
- What does the [Flags] Enum Attribute mean in C#?
- mysql 查看表占用空间的大小
- ios UIKit框架分析 第2天
- object-c 学习(面向对象)
- ORA-00445: background process "J000" did not start after 120 seconds
- java 正则表达式量词
- java代码动态生成控件
- html、htm、shtml
- 【容易打工网】职场领悟八句话可帮你少奋斗十年