Previously, if we defined an enum type and then defined an enum variable, then this enum variable could only be one value in the type, now we want one variable to represent multiple values:
When I was reading "Pro Net 2.0 Windows Forms And Custom Cortrols In C#" today, I saw an enumeration section, and I found that I needed to merge multiple values in an enum, and I saw that "|" was used. operator, I didn't pay much attention to it before, but today I thought about why I used "|" What about it?
I saw this sentence in MSDN: "Enum constants are defined by powers of 2 (i.e. 1, 2, 4, 8, etc.). This means that the individual flags in the combined enum constants do not overlap. ”
(The enum constant must be defined by powers of 2 (i.e., 1, 2, 4, 8, etc.). This means that the individual flags in the combined enum constants do not overlap.)
If you can have more than one value for a value, you can use an enum and add Flags
This article tells you how to write a Flags.
Before writing, you need to know some basic knowledge, take the opposite, or, and, if you don't know, please take a look at the basics.
Of course, these are too complicated for me to explain here.
If there is a type
Merge multiple values
To merge multiple of them, use |
Determine if a value exists
An easy way to do this is with HasFlag, but one way is to use &
Remove a value
Take the opposite value
|