What does the [Flags] Enum Attribute mean in C#?
2014-05-12 14:37
435 查看
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:
Note that
at all - all it does is enable a nice representation by the
It is also important to note that
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:
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:
To retrieve the distinct values in you property one can do this
or, in .NET 4 and later,
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)
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
So when you retreive the value you are actually bitwise AND'ing the values
The None = 0 value
And regarding use 0 in you enumeration, quoting from msdn:
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
转载地址:http://stackoverflow.com/questions/8447/what-does-the-flags-enum-attribute-mean-in-c
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:
[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: 00001110MyColor.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
转载地址:http://stackoverflow.com/questions/8447/what-does-the-flags-enum-attribute-mean-in-c
相关文章推荐
- What does the distance attribute in DMatches mean?
- What does idl attribute mean in the W3C html5 standard document?
- What does the 11 mean in INT(11)?
- what does the three dots in “doInBackground(URL… urls)” mean?
- What does the first letter "c" mean in "calloc"?
- So, what does the asterisk(*) mean in AssemblyVersion("1.0.*")?
- why we use _MainText_ST and what does the mean of TRANSFORM_TEX
- What does var that = this; mean in javascript?
- __memcpy_sse2_unaligned - what does this mean in detail?
- what does mean "Units" in apple app sales report?
- What does information_schema.TABLES.DATA_FREE mean in MySQL?
- What does (Aborted) mean in HttpWatch?
- What does "size" in int(size) of MySQL mean?
- What does Using filesort mean in MySQL?
- What does backend type static mean in Magento?
- What does ^M character mean in Vim?
- What does '>/dev/null 2>&1' mean in this article of crontab basics
- Bunder: What does :require => nil in Gemfile mean?
- What dose "%...D The time taken to serve the request, in microseconds." mean?
- What does it mean to write `static void * ptr = &ptr` in objective-c?