Enums enumerations are value types, and data is stored directly in the stack, rather than in isolation using references and real data.
(1) By default, the first variable in the enumeration is assigned a value of 0, the values of the other variables are incremented by the defined order (0,12,3 ...), so the following two code definitions are equivalent:
[CSharp]View Plaincopy
- Enum TrafficLight
- {
- Green,
- Yellow,
- Red
- }
[CSharp]View Plaincopy
- Enum TrafficLight
- {
- Green = 0,
- Yellow = 1,
- Red = 2
- }
(2) The names of variables of enum enum types cannot be the same, but the values can be the same, for example:
[CSharp]View Plaincopy
- Enum TrafficLight
- {
- Green = 0,
- Yellow = 1, //Duplicate value, OK
- Red = 1 //Duplicate value, OK
- }
(3) If some of the members in the enum explicitly define the value and the part does not, the member that does not define the value is incremented by the value of the previous member, for example:
[CSharp]View Plaincopy
- Enum Looptype
- {
- None, //value is 0
- Daily, //value is 1
- Weekly = 7,
- Monthly, //value is 8
- Yeayly, //value is 9
- Daygap = 15,
- Weekgap, //value is
- Monthgap, //value is
- Yeargap //value is
- }
(4) Enum enumeration members can be used as bit flags while supporting bitwise operations (BITS and bits or so), for example:
[CSharp]View Plaincopy
- Enum carddecksettings: uint
- {
- Singledeck = 0x01, //Bit 0
- Largepictures = 0x02, //Bit 1
- Fancynumbers = 0x04, //Bit 2
- Animation = 0x08 //Bit 3
- }
Summary of enum usages in C #