Enumeration in C#

Unlike the variables discussed thus far, an enumeration is not a type in itself but a special form of a value type. An enumeration is derived from System.Enum and supplies names for values. The underlying type that an enumeration represents must be a byte, short, int, or long.
Each field within an enumeration is static and represents a constant.

To declare an enumeration, you must provide the keyword enum followed by the name of the enumeration. Then you must provide an opening bracket followed by a list of the enumeration strings, and end with a closing bracket, as shown in the following example:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication2

{

    class Program

    {

        static void Main(string[] args)

        {

            int MyInteger = (int)Pizza.Supreme + (int)Pizza.MeatLovers + (int)Pizza.CheeseLovers + (int)Pizza.Vegetable;

            Console.WriteLine("Total enum value is " + MyInteger);

            Console.ReadLine();

        }

        public enum Pizza

        {

            Supreme = 2,

            MeatLovers = 3,

            CheeseLovers = 4,

            Vegetable = 5,

        }

    }

}

Ebook Download
View all
Learn
View all