Summary of enum usages in C #

Source: Internet
Author: User

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
    1. Enum TrafficLight
    2. {
    3. Green,
    4. Yellow,
    5. Red
    6. }
[CSharp]View Plaincopy
    1. Enum TrafficLight
    2. {
    3. Green = 0,
    4. Yellow = 1,
    5. Red = 2
    6. }

(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
    1. Enum TrafficLight
    2. {
    3. Green = 0,
    4. Yellow = 1, //Duplicate value, OK
    5. Red = 1 //Duplicate value, OK
    6. }
(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
  1. Enum Looptype
  2. {
  3. None, //value is 0
  4. Daily, //value is 1
  5. Weekly = 7,
  6. Monthly, //value is 8
  7. Yeayly, //value is 9
  8. Daygap = 15,
  9. Weekgap, //value is
  10. Monthgap, //value is
  11. Yeargap //value is
  12. }
(4) Enum enumeration members can be used as bit flags while supporting bitwise operations (BITS and bits or so), for example:

[CSharp]View Plaincopy
    1. Enum carddecksettings: uint
    2. {
    3. Singledeck = 0x01, //Bit 0
    4. Largepictures = 0x02, //Bit 1
    5. Fancynumbers = 0x04, //Bit 2
    6. Animation = 0x08 //Bit 3
    7. }

Summary of enum usages in C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.