c# - What is the convention for defining values for enum flags? -


let's have following code:

[flags] enum myflags {     none = 0,     = 1,     b = 2,     c = 4,     d = 8,     e = 16,     // ... } 

this not going optimal when amount of flags grow large. , optimal, mean readable, not fast or memory saving.

for combined flags, such as

ab = 3 

we can use

ab = | b 

instead, more readable.
, combining flags

all = | b | c | ... 

it more favorable use

all = ~none 

instead, if don't make full use of 32/64 bits available.

but regular values?

between

e = 16 e = 0b10000 e = 0x10 e = 1 << 4 

or other ways haven't thought of, best suited large amount of flags?

or in other words, (agreed upon) convention setting values flags in c#?

let assume sake of argument, values not aligned, code might this

none = 0, apple = 1, banana = 2, strangefruit = apple | banana, cherry = 4, date = 8, elderberry = 16, // ... 

for regular values, 1 << n: after you've taken (short) time understand it, it's easy see what's going on, hard mess up, , requires no hex/binary conversion/thinking.

[flags] enum myflags {     none = 0,     = 1 << 0,     b = 1 << 1,     c = 1 << 2,     d = 1 << 3,     e = 1 << 4,     // ...     lastly = 1 << 31, } 

as far actual defined convention, don't think 1 exists. ms's enum design guidelines says

√ do use powers of 2 flag enum values can freely combined using bitwise or operation.

but not specify how write in source (those language-agnostic guidelines; might in c# might not work in .net language).


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -