1. Overview
This chapter contains enumeration types, bit flags, and methods for enumeration types.
2. Main content
2.1 Enum types
An enumeration type defines a set of "symbol name/value" pairs.
The benefits of enumeration types include: ① can make programs easier to write, read, and maintain. The ② enumeration type is strongly typed and has compiler detection.
When compiling enumeration types, the C # compiler converts each symbol into a constant field of type.
The System.Enum type has a static method named Getunderlyingtype that returns the underlying type that holds the value of an enumerated type.
Enum.getunderlyingtype (typeof(Color));
2.2-bit flag
An enumeration type represents a single numeric value, while a bit flag represents a set of bits, some of which are in the on state, and some bits are in the off state.
Using the [Flags] attribute, you can ensure that the correct enumeration value is returned after the bitwise operation.
Never use the IsDefined method on a bit flag enumeration type.
2.3 Adding a method to an enumeration type
You can use the extension method functionality of C # to simulate adding methods to enum types.
3. Summary
This chapter describes some of the less common features of enumeration types.
Reasonable use of bit marks can greatly improve efficiency.
15th Zhang Yi type and bit flags