HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
An enum is a special "class" that represents a group of constants (unchangeable/read-only variables)
An enum
is a special "class" that represents a group of
constants (unchangeable/read-only variables).
To create an enum
, use the enum
keyword (instead of class or interface), and separate
the enum items with a comma:
enum Level
{
Low,
Medium,
High
}
You can access enum
items with the dot syntax:
Level myVar = Level.Medium;
Console.WriteLine(myVar);
Enum is short for "enumerations", which means "specifically listed".
You can also have an enum
inside a class:
class Program
{
enum Level
{
Low,
Medium,
High
}
static void Main(string[] args)
{
Level myVar = Level.Medium;
Console.WriteLine(myVar);
}
}
The output will be:
Medium
By default, the first item of an enum has the value 0. The second has the value 1, and so on.
To get the integer value from an item, you must explicitly convert the item to an
int
:
enum Months
{
January, // 0
February, // 1
March, // 2
April, // 3
May, // 4
June, // 5
July // 6
}
static void Main(string[] args)
{
int myNum = (int) Months.April;
Console.WriteLine(myNum);
}
The output will be:
3
You can also assign your own enum values, and the next items will update the number accordingly:
enum Months
{
January, // 0
February, // 1
March=6, // 6
April, // 7
May, // 8
June, // 9
July // 10
}
static void Main(string[] args)
{
int myNum = (int) Months.April;
Console.WriteLine(myNum);
}
The output will be:
7
Enums are often used in switch
statements to check for corresponding values:
enum Level
{
Low,
Medium,
High
}
static void Main(string[] args)
{
Level myVar = Level.Medium;
switch(myVar)
{
case Level.Low:
Console.WriteLine("Low level");
break;
case Level.Medium:
Console.WriteLine("Medium level");
break;
case Level.High:
Console.WriteLine("High level");
break;
}
}
The output will be:
Medium level
Use enums when you have values that you know aren't going to change, like month days, days, colors, deck of cards, etc.