I’ve declared an enum type,
assigned a variable to it
and now I am writing it to the console.
So what use does an enum type have in a real world application?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Enum
{
enum cars
{
Toyota, Nissan, Ferrari, Lamborghini
}
}
class Program
{
enum cars
{
Toyota, Nissan, Ferrari, Lamborghini
}
static void Main(string[] args)
{
int a = (int)cars.Ferrari;
Console.WriteLine(a);
}
}
}
Whenever a procedure accepts a limited set of variables, consider using an enumeration. Enumerations make for clearer and more readable code, particularly when meaningful names are used.
The benefits of using enumerations include:
Reduces errors caused by transposing or mistyping numbers.
Makes it easy to change values in the future.
Makes code easier to read, which means it is less likely that errors
will creep into it.
Ensures forward compatibility. With enumerations, your code is less
likely to fail if in the future someone changes the values
corresponding to the member names.
ref : MSDN