In C#, an enum is a short form of enumeration. It is a specific type that represents a set of named constant values. Enums are typically used to define a set of related values that have a fixed number of possible choices, such as days of the week, months of the year, or colors.
There are several advantage or reasons why you might want to use enums in C#:
In this example, the MonthofYear enum defines a set of twelve named constants, each of which represents a month of the year.
The following defines an enum for the yearmonths.
enum YearMonths
{
January,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December
}
Now access the enum in program.
using System;
namespace FirstProgram
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(YearMonths.January);
Console.WriteLine(YearMonths.October);
}
enum YearMonths
{
January,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December
}
}
}
Output