[Flags]
public enum MyEnum
{
read = 1,
write = 2,
create = 4,
delete = 8,
rwcd = read | write | create | delete
}
This kind of defeats the purpose of what an enum with the FlagsAttribute represents, doesn't it?
Its ToString() method wouldn't return the string "read, write, create, delete" but "rwcd" instead, when encountering the combined value. If I'm interpreting the doc correctly, this is all that the FlagsAttribute does.
Did you read the doc you refer to above?
Specifically, under "Guidelines for FlagsAttribute and Enum", the third bulleted item:
Consider creating an enumerated constant for commonly used flag combinations. For example, if you have an enumeration used for file I/O operations that contains the enumerated constants Read = 1 and Write = 2, consider creating the enumerated constant ReadWrite = Read OR Write, which combines the Read and Write flags. In addition, the bitwise OR operation used to combine the flags might be considered an advanced concept in some circumstances that should not be required for simple tasks.
Beyond that, an enumerated constant can be the logical union of several other constants, but can still have a different meaning beyond that, as is the case in the MatchPropFlags enum I posted above.
Here it is again, with the revision that I had to make when entity Transparency was introduced:
This was the enum
before the Entity.Transparency property was introduced:
[Flags]
public enum MatchPropFlags
{
Color = 0x1,
Layer = 0x2,
Linetype = 0x4,
Thickness = 0x8,
LinetypeScale = 0x10,
Text = 0x20,
Dimension = 0x40,
Hatching = 0x80,
Lineweight = 0x100,
PlotStyleName = 0x200,
Polyline = 0x400,
Viewport = 0x800,
Table = 0x1000,
Material = 0x2000,
ShadowDisplay = 0x4000,
Multileader = 0x8000,
All = 0xFFFF,
Entity = Color | Layer | Linetype | Thickness
| LinetypeScale | Material | Lineweight
| PlotStyleName | ShadowDisplay
};
When the Entity.Transparency property was introduced, the Transparency bit flag was added, and the Entity constant was modified to include it:
Transparency = 0x10000,
Entity = Color | Layer | Linetype | Thickness
| LinetypeScale | Material | Lineweight
| PlotStyleName | ShadowDisplay | Transparency
The point to the Entity constant (which like the rwcd constant in the example you quoted), is to specify that
all Entity properties are to be matched, in a non-literal way. IOW, it means all entitiy properties,
including any that are added in the future, which is not exactly the same as 'all entity properties that existed when the enum was defined or used'.
So, when the Transparency flag was added to support entity transparency in MATCHPROPS, all existing code that used the Entity member, rather than explicitly specifying each of the flags it represents, automatically supported matching entity Transparency
without having to be revised.
If in my code where this enum was used, I instead used the individual flags when the intent was to match
all entity properties, then I would have had to revise each place where those flags were used, and add the Transparency flag.
So, in this case the Entity member has its own meaning, in spite of the fact that its value is the logical or of all the flags for each Entity's property. Perhaps its meaning would be clearer if I had given it the name "AllSupportedEntityProperties" ?
In the case of the example you quoted, it could have the meaning "full permissions", which could make its meaning slightly different then read+write+create+delete, as well.
The FlagsAttribute itself doesn't really do anything, but it is recognized in a number of places in the framework. For example, the PropertyGrid will behave differently when that attribute is applied to a property of an Enum type. There are also cases where data binding might specifically look for the flag to bind to a specific field, rather than the aggregated value.
[I revised this post after having read the referenced docs]