/ Published in: C#

FlagsAttribute is useful to render a string indicating which status bits or flag bits are turned on. The resulting string will contain the name of the enum bit that is turned on without needed to maintain a separate string table.
Note the inheritance-like syntax that determines the size of the enum. This is useful to adjust the size of enum structure members for marshaling with C.
Note the inheritance-like syntax that determines the size of the enum. This is useful to adjust the size of enum structure members for marshaling with C.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
// Given this enum... [FlagsAttribute] enum MyStatus: ushort { Bit0 = 0x0000, Bit1 = 0x0001, Bit2 = 0x0002, Bit3 = 0x0004 }; // Cast a 0x7 to MyStatus and render a string for testing/demonstration purposes: ((MyStatus)0x7).ToString() results in string "Bit0, Bit1, Bit2" // To test a bit, use code of the form. Assumes status is a MyStatus. if ( Convert.ToBoolean( status & MyStatus.Bit0) )
URL: http://msdn.microsoft.com/en-us/library/system.flagsattribute%28VS.71%29.aspx
Comments
