The example below shows how flags can be used to work with bitwise combinations can be. Several flags can be associated with an OR operator (See line 14). A combination can be resolved by an AND operator (See, for example, line 19).
a [Flags()]
2
public
enum
Usage
3 {
4 Private = 1,
5 Commercial = 2,
6 Education = 4,
7 Training = 8
8 }
9
10
class
Program
11 {
12
static
void Main( string [] args)
13 {
14 TheUsage( Usage .Commercial .Private & usage) == Usage .Private; // false
20
bool
isCommercial = (
.Commercial & usage) == Usage .Commercial; // true
21
bool isEducation = ( Usage .Education & usage) == Usage .Education;
//
22 isTraining bool = (. Usage Training & usage) == Usage training;. / / false
23 } 24 }
I love it.
0 comments:
Post a Comment