Reputation:
I'm browsing an open source .NET twain wrapper and saw this:
[Flags]
internal enum TwDG : short
{ // DG_.....
Control = 0x0001,
Image = 0x0002,
Audio = 0x0004
}
What exactly does that 'Flag' decorator mean? (Is it called a 'decorator'?)
Also, what does the short mean at the end of the enum declaration?
Thanks!
Upvotes: 3
Views: 352
Reputation: 5773
It means that you give a hint, that this enum will be used for "bitwise or" operations
var flags = TwDG.Control | TwDG. Image;
Console.WriteLine(flags.HasFlag(TwDG.Image)); // true
Console.WriteLine(flags.HasFlag(TwDG.Control)); // true
Console.WriteLine(flags.HasFlag(TwDG.Audio)); // false
More info FlagAttribute (Enum.HasFlag
was added in Framework 4.0)
Short is saying, that back-type for this enum is not int (which is default option for enums), but short. Also you can specify long, ushort, or any other integer built-in type.
Upvotes: 2
Reputation: 38434
It's an attribute. Although others have said that it is necessary so that you can perform bit flipping operations with the enum, this is not true. You can do this with enums without this attribute.
If you have applied the attribute, you get a different ToString() output on the enum which will pretty-print the combined members of a enum value, e.g. "Blue | Red | Orange", instead of "7".
The "short" keyword means that the type for the enum members will be a 16-bit signed integer.
Upvotes: 3
Reputation: 13350
I've heard them called decorators before (and it is acceptable to label them as such in the community) but for arguments sake and strictly speaking; it is an attribute. It is used to "mark" the enum as a bit flag type.
Here's the MSDN Reference.
Upvotes: 0
Reputation: 437336
Flags
is an attribute; specifically, System.FlagsAttribute
.
It means that the compiler lets you use values of type TwDG
as a bit-field, i.e., store as many of them as you want in one value like this:
var control = TwDG.Control;
var allTogether = TwDG.Control | TwDG.Image | TwDG.Audio;
Typically, this is done when some code needs to take different (or optional) actions depending on if one of these flags is set. For example, let's say we want to describe the contents of a video file, which might contain audio and picture. You could write:
var imageAndAudio = TwDG.Image | TwDG.Audio;
var muteImage = TwDG.Image;
Then, if you wanted to check if the file contains an audio track, you would "pick out" the Audio
flag like this:
var hasAudio = (myValue & TwDG.Audio) != (TwDG) 0;
Upvotes: 0
Reputation: 105019
This makes an enumeration a bit-flag.
It means you can combine individual values together.
Like:
TwDG value = TwDG.Control | TwDG.Image | TwDG.Audio;
Which would give it a value of 7.
Individual enumeration values usually have a value of 2^n
. But can as well be combined like:
[Flags]
public enum Sides
{
Left = 1,
Right = 2,
Up = 4,
Down = 8,
LeftAndRight = 3,
UpAndDown = 12,
AllSides = 15
}
Upvotes: 1
Reputation: 3962
short is another keyword for System.Int16, a two-byte integer ranging from -32,768 to 32,767. By default, an enum's base type is int; in this case, they're attempting to use a smaller data type to store the enumerator values.
Upvotes: 1
Reputation: 450
As for [Flag] - you should look here link text
Short - data time, which used to store enum values.
Upvotes: 0
Reputation: 3071
the short means that the enum is using short instead of an int as its base type. as for the flags http://msdn.microsoft.com/en-us/library/system.flagsattribute.aspx
Upvotes: 1
Reputation: 17808
Its the flag attribute, you can read up on it here:
http://msdn.microsoft.com/en-us/library/cc138362.aspx
Lets you treat a set of enums a bit flag set.
Upvotes: 1
Reputation: 63126
The Flags Attribute is used to allow and decorate the enumeration for bitwise math operations on enum values.
Doing this allows you to add them together, or other operation items.
The Short part defines it as a Short rather than an integer, detail on this is also in the linked URL
Upvotes: 8