C
C#3w ago
Garel

Question about flag enums

[Flags]
public enum LogFlags
{
UseHeader,
UseDateTime,
ShouldSave,
}
[Flags]
public enum LogFlags
{
UseHeader,
UseDateTime,
ShouldSave,
}
will these be set respectively as 1, 2, 4, 8... (in powers) of two automatically?
6 Replies
reflectronic
reflectronic3w ago
no you have to assign the values yourself
canton7
canton73w ago
All [Flags] does (besides telling humans what you're up to) is alter how ToString() works
Angius
Angius3w ago
Nifty trick: use bitshifting to not have to think about PoT
[Flags]
enum Foo
{
A = 1 << 0, // 1
B = 1 << 1, // 2
C = 1 << 2, // 4
D = 1 << 3, // 8
E = 1 << 4, // 16
}
[Flags]
enum Foo
{
A = 1 << 0, // 1
B = 1 << 1, // 2
C = 1 << 2, // 4
D = 1 << 3, // 8
E = 1 << 4, // 16
}
333fred
333fred3w ago
Personally, I prefer binary literals there
Aaron
Aaron3w ago
or - that
Garel
GarelOP3w ago
Thanks

Did you find this page helpful?