Summary of Enum usage in C #

Source: Internet
Author: User

Enums enumeration is a value type. Data is directly stored in the stack, instead of being stored using reference and real data isolation.
(1) by default, the first variable in the enumeration is assigned 0, and the values of other variables are incremented in the defined order (, 3 ...), therefore, the following two code definitions are equivalent:
[Csharp]
Enum TrafficLight
{
Green,
Yellow,
Red
}
[Csharp]
Enum TrafficLight
{
Green = 0,
Yellow = 1,
Red = 2
}

(2) the names of enum-type variables cannot be the same, but the values can be the same. For example:
[Csharp]
Enum TrafficLight
{
Green = 0,
Yellow = 1, // Duplicate value, OK
Red = 1 // Duplicate value, OK
}
(3) if some members in the enum explicitly define the value but some do not, the value of the member who does not define the value is assigned progressively according to the value of the previous Member. For example:
[Csharp]
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 16
MonthGap, // value is 17
YearGap // value is 18
}
(4) enum enumeration members can be used as bit flags and support bit operations (bit and, bit or so on), such:
[Csharp]
Enum CardDeckSettings: uint
{
SingleDeck = 0x01, // Bit 0
LargePictures = 0x02, // Bit 1
FancyNumbers = 0x04, // Bit 2
Animation = 0x08 // Bit 3
}
A hexadecimal number is used for bitwise operations and operations, which is very convenient.


 

Related Article

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.