c# – Create a variable that can only hold a fixed set of values (aka an Enum Constant)

A enum constant is a special kind of variable that can only take a set number of values, e.g.:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp
{
    class EnumConstantDemo
    {
        // notice that you have to place this outside the method, othewise the method
        // wont recognise this a enum constant (aka variable type)
        enum Colour
        {
            red,
            green,
            blue      // notice no comma for the last one. 
        }

        static void Main(string[] args)
        {
           
            Colour Favourite = Colour.green;
            
            switch (Favourite)        
            {
                case Colour.green:
                    Console.WriteLine("Your fave colour is green");
                    break;
                case Colour.red:
                    Console.WriteLine("Your fave colour is red");
                    break;
                case Colour.blue:
                    Console.WriteLine("Your fave colour is blue");
                    break;
                default:
                    Console.WriteLine("Sorry don't know your fave colour");
                    break;
            }
            
            Console.WriteLine("Switch statement has ended");
        }
    }
}

The cool thing about this aproach is that VS2013 will recognise the enum constant as type of variable, and hence will offer intellisense, e.g.: