Replies: 12 comments
-
An alternative, to save "working out" powers of 2, would be to use binary literals: [Flags]
public enum AccessRights
{
None = 0,
Read = 0b0001,
Write = 0b0010,
Save = 0b0100,
Validate = 0b1000
} |
Beta Was this translation helpful? Give feedback.
-
I find using shift operators leads to more readable (and easily reviewable) code: [Flags]
public enum AccessRights
{
None = 0,
Read = 1 << 0,
Write = 1 << 1,
Save = 1 << 2,
Validate = 1 << 3
} |
Beta Was this translation helpful? Give feedback.
-
@nil4 method is good, but what if you need to use an intermediate value (eg: ReadWrite) ? Then int values are more readable again... This is off-topic, and goes in the sense that if there is no need to have intermediate values, there is no need either to specify values for a high-level flag. |
Beta Was this translation helpful? Give feedback.
-
@Leogiciel what about |
Beta Was this translation helpful? Give feedback.
-
I totally agree @FredyFerrari. I was just arguing about the unneeded technical consideration of assigning values to a bitfield... |
Beta Was this translation helpful? Give feedback.
-
@Leogiciel but even your example would be a bit tricky: AccessRights.None would then not be a flag, right? It would be more like ReadWrite or All, not? |
Beta Was this translation helpful? Give feedback.
-
@FredyFerrari considering this MSDN article, None seems to be a good practice as a default value, since All seems not (All is just the addition of all flags, 15 in my example, not 16). I just think a flag keyword implementing best practices (None = 0, First=1, Second=2, Third=4, (...)) would save a lot of time to developpers with big enum flags, encapsulating the technical logic of the bitfield enum. |
Beta Was this translation helpful? Give feedback.
-
@Leogiciel so you have a maximum of 33 flags in a int32 based enum? None, Flag0, Flag1 .. Flag31? Sounds kind of wired, not? ;) |
Beta Was this translation helpful? Give feedback.
-
Most of my flags enums do not have a |
Beta Was this translation helpful? Give feedback.
-
Maybe because 0 is the default value ?
|
Beta Was this translation helpful? Give feedback.
-
@Leogiciel So the real question is, will people be able to commonly use your proposed sequence? Or will everyone be opting out all the time? To have value, the sequence needs to address an overwhelmingly common pattern. |
Beta Was this translation helpful? Give feedback.
-
I'm fine with |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Like the enum keyword, but with flag values by default.
Actually, this enum:
Assign value 3 to Save, which isn't the desired behaviour for a flag.
This one:
...does the trick, but it is low-level coding because developpers should compute every powers of two which seems "old school", especially for long enums.
Would love to see a "flag" keyword encapsulating the bitfield logic:
Cheers :)
Beta Was this translation helpful? Give feedback.
All reactions