C# ClassesLesson 34Free preview
C# Enums
This lesson is part of C# · All levels
Summary
An enum is a special type that holds a set of numerically-valued constant names, e.g. a day enum with Monday and Tuesday members. With the sixth, the magic number is renamed meaningfully so that the code is more readable. The value of enum can be converted to a number and vice versa, and supports a combination of values with the attribute of Flags.
Key points
- Enum is defined by the keyword enum, and each member is worth int starting from 0 automatically.
- The explicit value can be given as the enum of Level {Low = 1, High = 5}.
- The enum can be used as a type of variable and a parameter so that the code is more secure than the normal int.
- Conversion uses casting, for example (int) color to take its numerical value.
- The Flags attribute allows combining several enum values using a pipe operator.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.