An enum, short for enumeration, is a distinct data type in C# that consists of a set of named constants called enumerators.
Enums provide a way to define a group of related named constants, making the code more readable and maintainable.
Enums provide a way to define a group of related named constants, making the code more readable and maintainable.
using System;
// Define an enum named DaysOfWeek
public enum DaysOfWeek
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
class Program
{
static void Main(string[] args)
{
// Using enums to represent days of the week
DaysOfWeek today =
DaysOfWeek.Friday;
// Output the value of today
Console.WriteLine($"Today is: {today}");
// Checking if today is a weekend day
if (today ==
DaysOfWeek.Saturday || today == DaysOfWeek.Sunday)
{
Console.WriteLine("It's the
weekend!");
}
else
{
Console.WriteLine("It's a weekday.");
}
}
}