<aside> Enumerations (enums) are strongly typed constants that help make code more readable and maintainable by giving meaningful names to a set of related values.
</aside>
Enums are value types that define a set of named constants. They're perfect for representing fixed sets of options or states.
public enum DaysOfWeek
{
Sunday, // 0
Monday, // 1
Tuesday, // 2
Wednesday, // 3
Thursday, // 4
Friday, // 5
Saturday // 6
}
<aside> 💡 By default, enum values start at 0 and increment by 1, but you can assign custom values!
</aside>
public enum Permissions
{
None = 0,
Read = 1,
Write = 2,
Execute = 4,
All = Read | Write | Execute
}
// Declaring enum variable
DaysOfWeek today = DaysOfWeek.Friday;
// Converting enum to string
string dayName = today.ToString(); // "Friday"
// Converting string to enum
DaysOfWeek day = Enum.Parse<DaysOfWeek>("Monday");
// Getting all enum values
DaysOfWeek[] allDays = Enum.GetValues<DaysOfWeek>();
// Checking if value exists
bool isValid = Enum.IsDefined(typeof(DaysOfWeek), "Sunday");