Write in front
Enumeration Enumeration (Enumeration), a unique type composed of a set of named constants called lists of enumerations. It can be seen that in order to facilitate the use of some constants with specific values in the program, everyone is familiar with the general use. This article mainly introduces the feature of enumeration, FlagAttribute.
What is FlagAttribute?
Flag feature Microsoft's explanation is: It indicates that enums can be processed as bit domains (that is, a set of flags). The FlagsAttribute attribute is an optional attribute of the enum type. Its main function is to process enums as bit domains (. C# does not support bit domains). The so-called bit domain is a collection of adjacent binary bits in a single memory cell. By adding this property to the enum, some behaviors of the enum can be changed to meet our needs.
For example, we have the following enum definition:
public enum OrderTypeEnum { Init, Complete, Waiting, Paid }
I believe everyone is familiar with logic or operations. For integers, | operations are to convert them into binary and then perform or operations. | The work done is actually 0001 | 0010 = 0011 = 3 and then convert it to (OrderTypeEnum)3.
If we do | operation on two enum values, what will the result be?
OrderTypeEnum result = | ;
According to the principle of or operation: 0010 | 0011 = 0011 (3) Paid , in essence, the result we want is to say that both enum values are the result of or operation, but because the enum values are incremented from 0 by default, then after or operation, the result we want cannot be obtained. What should I do? At this time, we need to add [Flags] to the enum. Let's first look at the criteria defined by FlagsAttribute:
- Using FlagsAttribute enumeration is a custom property that performs bitwise operations (AND, OR exclusive or) on numeric values.
- Define enum constants in powers of 2, i.e. 1, 2, 4, 8, etc. This means that the individual flags of the enum constants combined in do not overlap.
- Consider creating constants for enumerations for commonly used flag combinations. For example, if you have an enum for file I/O operations that contains constants Read = 1 and Write = 2 for enums, consider creating constants ReadWrite = Read OR Write for enums, which combines Read and Write flags. Additionally, bitwise OR operations that can be used to combine flags are considered as, in some cases, not a high-level concept required for simple tasks.
- Be careful if the enumeration constant is defined as a negative number, because many flag positions may be set to 1, which may confuse your code and encourage encoding errors.
- A simple way to test whether to set a flag in a numeric value is to perform a bitwise, numeric value between operations and flag enumeration constants, which set all bits to a numeric value that does not correspond to zero of the flag, and then test whether the result of the operation is equal to the flag enumeration constant.
- Use None as the flag name to enumerate constants whose value is zero. You cannot use None bitwise operation to test a flag because the result is always zero for the enumeration constant. However, you can perform logic between bitwise, comparison, and None enumeration constants to determine whether any bits have been set in the numeric value.
- If you create an enum of value instead of flag enumeration, it is still necessary to create a constant of None enumeration. The reason is that by default the memory used for enumeration is initialized to zero for common language runtime. Therefore, if a constant whose value is zero is not defined, the enum will contain illegal values at creation time.
- If your application needs to represent obvious by default, consider using a constant for enums whose value is zero to represent the default value. If there is no default, consider using a constant of an enum whose value is zero means that this is not represented by any other enum constant.
- An enum value is not defined, just for mirroring the state of the enum itself. For example, the constants of an enum for only the end tag of an enum are not defined. If you need to determine the last value in the enum, check the value explicitly. Additionally, you can perform range checks for the first and last enumeration constants if all values in the range are valid.
- Do not specify constants that retain future enumerations.
- When you define a method or property that takes a constant as an enumeration of values, consider verifying the value. The reason is that even if the value is not defined in the enum, you can cast it to a numeric value of the enum type.
We see the second sentence tells us that when the Flags feature is added, the default enum value will be incremented at one time in power of 2, such as 20, 21, 22, 23 (1, 2, 4, 8...)
So let’s re-look at what will happen after the redefinition or operation?
[Flags] public enum OrderTypeEnum { Init, Complete, Waiting, Paid }
Now let’s look at it again: OrderTypeEnum result = | | ;
0010 | 0100 | 1000 = 1110 We can see that in essence, it is done binary or operation, and all bit values are combined
When we can use bit operations, not only yes, or, non, x-or, etc. operations can be implemented.
We know that in this way, we can merge the enum values OrderTypeEnum result = | | ;
Then the same can be used to determine whether such a set contains an enum value:
()
Written at the end
Enumeration adds the feature of Flags to enable it to have the ability to operate bits, which makes it more convenient for us to use in daily code.
Reference: /2015/07/26/enum-flags-and-bitwise-operators/